使用共享模块中的spring-beans .xml文件(位于“resources”文件夹中)与gradle多模块(子项目)

cs7cruho  于 2023-02-04  发布在  Spring
关注(0)|答案(1)|浏览(124)

(To我在这个问题的底部有github链接)
我有(并且正在"开始"低年级多模块/项目)
settings.gradle

rootProject.name = 'com.mycompany.myteam.myapp-rootProjectName'

include ':source:java:myapproot:myapp-toplayer-console-di-xml-one'
include ':source:java:myapproot:myapp-business-logic'
(maybe even a few that are purely .java/.class "code logic" libraries)

在子项目中:
':源代码:java:我的批准:我的应用程序到播放器控制台-di-xml-one'
我有一个典型的Spring-Boot启动程序和一个使用applicationContext.xml的spring-bean设置。

./source/java/myapproot/myapp-toplayer-console-di-xml-one/src/main/java/demo/SpringBootApplicationContextXmlConsoleApplication.java
--
./source/java/myapproot/myapp-toplayer-console-di-xml-one/src/main/resources/applicationContext.xml
./source/java/myapproot/myapp-toplayer-console-di-xml-one/src/main/resources/orch.one.di.xml
./source/java/myapproot/myapp-toplayer-console-di-xml-one/src/main/resources/clientporxy.one.di.xml

"applicationContext.xml"的内容如下。注意,它有一个指向其他一些. xml文件的"指针""导入"条目。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="orch.one.di.xml"/>

    <import resource="clientproxy.one.di.xml"/>

</beans>

上面的一切都很好,我已经为许多不同的项目做了很多年了。

我尝试做的是将2个"额外"*. di. xml文件放入另一个gradle模块中。
settings.gradle

rootProject.name = 'com.mycompany.myteam.myapp-rootProjectName'

include ':source:java:myapproot:myapp-toplayer-console-di-xml-one'
include ':source:java:myapproot:myapp-business-logic'

    (new one below)
include ':source:java:myapproot:myapp-shared-resxex'

我想将 *. di. xml文件移动到这个不同的/新的gradle-subproject/模块中。(":source:java:myapproot:myapp-shared-resxex"需要说明)
现在我已经移动了两个 *. di. xml文件。
文件位置为:(在"资源"项下)

./source/java/myapproot/myapp-shared-resxex/src/main/resources/orch.one.di.xml
./source/java/myapproot/myapp-shared-resxex/src/main/resources/clientproxy.one.di.xml

所以现在当我启动"主"应用程序时,我会得到如下错误(我有点期待这些错误)

Caused by: java.io.FileNotFoundException: 

    ./source/java/myapproot/myapp-toplayer-console-di-xml-one/build/resources/main/clientproxy.one.di.xml

    (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
    at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
    at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
    at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:187)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333)

我已经"尝试"(并解决)了以下这些事情:
https://docs.gradle.org/current/userguide/cross_project_publications.html
Gradle: common resource dependency for multiple java projects
How module can use resources of another module using gradle multi-module
https://discuss.gradle.org/t/how-to-bundle-static-resources-from-other-subproject-into-java-application/42076
就像很多天,很多小时,一直试图找出神奇的酱。
所以,我的"一般"问题是

    • 如何让spring bean. xml文件在不同的gradle子项目/模块中工作?**

我将展示我的最新尝试:
./源代码/java/我的批准/我的应用程序共享的resxex/内部版本. gradle

configurations {
    myCoolConfigurationName
}

task tryToPackageStuffForOtherSubprojectsTask(type: Jar) {
    archiveClassifier
    from sourceSets.main.resources
}

artifacts {
    myCoolConfigurationName tryToPackageStuffForOtherSubprojectsTask
}

dependencies {

    implementation project(':source:java:myapproot:clientproxies:myclientproxy-root:myclientproxy-domain')
    implementation project(':source:java:myapproot:clientproxies:myclientproxy-root:myclientproxy-interfaces')
    implementation project(':source:java:myapproot:clientproxies:myclientproxy-root:myclientproxy-concrete')

}

然后:
./source/java/我的批准/我的应用程序到播放器控制台-di-xml-one/构建版本. gradle

plugins {
    id "application"
}

apply plugin : "java"

ext {
    javaMainClass = "demo.SpringBootApplicationContextXmlConsoleApplication"
}

application {
    mainClassName = javaMainClass
}

task myCustomPrintClasspathTask  {
    doLast {
        configurations.runtimeClasspath.each { println 'helloThere->' + it }
    }
}

dependencies {

    //the below line, if left not-commented-out, will produce a warning : "Execution optimizations have been disabled for task"
    implementation project(path: ':source:java:myapproot:myapp-shared-resxex', configuration: 'myCoolConfigurationName')

    implementation(group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBootVersion) {
        exclude module: "logback-classic"
    }

    implementation(group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: springBootVersion) {
        exclude module: "logback-classic"
    }

    implementation project(":source:java:myapproot:myapp-shared-resxex")
    implementation project(':source:java:myapproot:myapp-business-logic')

    implementation project(':source:java:myapproot:clientproxies:myclientproxy-root:myclientproxy-domain')
    implementation project(':source:java:myapproot:clientproxies:myclientproxy-root:myclientproxy-interfaces')
    implementation project(':source:java:myapproot:clientproxies:myclientproxy-root:myclientproxy-concrete')

    implementation group: 'org.apache.commons', name: 'commons-lang3', version: commonsLangVersion
    implementation group: 'javax.inject', name: 'javax.inject', version: "${javaxInjectVersion}"

    implementation group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion
    implementation group: 'org.slf4j', name: 'slf4j-simple', version: slf4jSimpleVersion

    testImplementation group: 'junit', name: 'junit', version: junitVersion
    testImplementation group: 'org.mockito', name: 'mockito-core', version: mockitoVersion

}

当我运行(一个干净的构建,然后)自定义任务时:

$ ./gradlew clean build
$ ./gradlew myCustomPrintClasspathTask

我得到以下输出:

> Task :source:java:myapproot:myapp-toplayer-console-di-xml-one:myCustomPrintClasspathTask

helloThere->./source/java/myapproot/myapp-shared-resxex/build/libs/myapp-shared-resxex.jar

helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter/2.7.5/c28e1546461803490588085345ba5d2897d232bc/spring-boot-starter-2.7.5.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.5/96646e63a2296d0a3209383e81cdb8c87ab2f913/spring-boot-autoconfigure-2.7.5.jar
helloThere->./source/java/myapproot/myapp-business-logic/build/libs/myapp-business-logic.jar
helloThere->./source/java/myapproot/clientproxies/myclientproxy-root/myclientproxy-concrete/build/libs/myclientproxy-concrete.jar
helloThere->./source/java/myapproot/clientproxies/myclientproxy-root/myclientproxy-interfaces/build/libs/myclientproxy-interfaces.jar
helloThere->./source/java/myapproot/clientproxies/myclientproxy-root/myclientproxy-domain/build/libs/myclientproxy-domain.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-lang3/3.12.0/c6842c86792ff03b9f1d1fe2aab8dc23aa6c6f0e/commons-lang3-3.12.0.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/javax.inject/javax.inject/1/6975da39a7040257bd51d21a231b76c915872d38/javax.inject-1.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-simple/1.7.36/a41f9cfe6faafb2eb83a1c7dd2d0dfd844e2a936/slf4j-simple-1.7.36.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-logging/2.7.5/61f4c53e35baa31a269bbeb7bb9d5e781448feef/spring-boot-starter-logging-2.7.5.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-to-slf4j/2.17.2/17dd0fae2747d9a28c67bc9534108823d2376b46/log4j-to-slf4j-2.17.2.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.slf4j/jul-to-slf4j/1.7.36/ed46d81cef9c412a88caef405b58f93a678ff2ca/jul-to-slf4j-1.7.36.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.36/6c62681a2f655b49963a5983b8b0950a6120ae14/slf4j-api-1.7.36.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.7.5/fd04e228e6e21b7ad13c10ae29afd31868d842e5/spring-boot-2.7.5.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/jakarta.annotation/jakarta.annotation-api/1.3.5/59eb84ee0d616332ff44aba065f3888cf002cd2d/jakarta.annotation-api-1.3.5.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/5.3.23/530b36b2ce2c9e471c6a260c3f181bcd20325a58/spring-context-5.3.23.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/5.3.23/30d0034ba29178e98781d85f51a7eb709a628e9b/spring-aop-5.3.23.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/5.3.23/3bdefbf6042ed742cbe16f27d2d14cca9096a606/spring-beans-5.3.23.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/5.3.23/3a676bf4b9bc42bd37ab5ad264acb6ceb63397a2/spring-expression-5.3.23.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.3.23/91407dc1106ea423c44150f3da1a0b4f8e25e5ca/spring-core-5.3.23.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.yaml/snakeyaml/1.30/8fde7fe2586328ac3c68db92045e1c8759125000/snakeyaml-1.30.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.3.23/3c7eb5fcca67b611065f73ff4325e398f8b051a3/spring-jcl-5.3.23.jar
helloThere->/Users/MyDomainUserName/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.17.2/f42d6afa111b4dec5d2aea0fe2197240749a4ea6/log4j-api-2.17.2.jar

如果我"偷看"jar:
./source/java/myapproot/myapp-shared-resxex/build/libs/myapp-shared-resxex.jar
我确实看到了两个 *. di. xml文件。
如果我跑了

$ ./gradlew run

我继续得到:

Caused by: java.io.FileNotFoundException: ./source/java/myapproot/myapp-toplayer-console-di-xml-one/build/resources/main/clientproxy.one.di.xml (No such file or directory)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
        at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
        at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
        at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:187)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333)
        ... 32 more

我的./gradle/wrapper/www.example.com文件(出于完整性和了解我的gradle版本的目的)gradle-wrapper.properties file (for completeness and to know my gradle-version)

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

.....
Github回购代码。
https://github.com/granadacoder/gradle-multi-with-spring-bean-proof
"main"分支...有一个工作的spring-beandi.xml设置。"main"分支与我上面的评论"上面的一切都很好"有关。
我有第二个分支(和一个PR,所以你可以看到"差异")。
分支机构:
https://github.com/granadacoder/gradle-multi-with-spring-bean-proof/tree/feature/move-spring-bean-xml-files-try-1
和PR(从功能分支到主分支,以显示"差异")
https://github.com/granadacoder/gradle-multi-with-spring-bean-proof/pull/2/files

3z6pesqy

3z6pesqy1#

好的。
我发现了一个"旧"文档页面。
https://docs.spring.io/spring-framework/docs/3.0.0.M4/reference/html/ch04s07.html
我将从上面的网址引用后,在情况下,它"消失"..特别是因为它是"旧"的文档。
//开始文章引用
4.7.2.2 prefix构造基于XML的应用程序上下文时,位置字符串可以使用特殊的类路径 *:前缀: prefix:
应用程序上下文ctx =新的类路径XML应用程序上下文("类路径 *:配置文件/应用程序上下文. xml");这个特殊的前缀指定必须获取与给定名称匹配的所有类路径资源(在内部,这基本上是通过ClassLoader. getResources(...)调用来完成的),然后合并以形成最终的应用程序上下文定义。
[注意]类路径 *:可移植性通配符类路径依赖于底层类加载器的getResources()方法。由于现在大多数应用服务器都提供自己的类加载器实现,因此在处理jar文件时,其行为可能会有所不同。检查类路径 * 是否工作的一个简单测试是使用类加载器从类路径上的jar中加载文件:getClass(). getClassLoader(). getResources("")。请尝试使用具有相同名称但位于两个不同位置的文件进行此测试。如果返回不适当的结果,请查看应用程序服务器文档,以了解可能影响类加载器行为的设置。
前缀"classpath *:"也可以与路径路径的其余部分中的PathMatcher模式结合使用,例如"classpath :META-INF/-beans. xml"。在这种情况下,解析策略相当简单:ClassLoader.getResources()调用用于最后一个非通配符路径段,以获得类加载器分层结构中的所有匹配资源,然后离开每个资源,上述相同的PathMatcher解析策略用于通配符子路径。
//结束文章引用
所以我忘记了使用类路径的"字符串魔术"(在文件名等之前)。
我能够将我的applicationContext.xml文件更新为以下内容。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- below are "imports" to OTHER (spring-bean) di.xml files -->

    <import resource="classpath:clientproxy.one.di.xml"/>

    <import resource="classpath*:orchestrators.one.di.xml"/>


</beans>

您可以在下面的SOF问题/答案中了解使用"*"(在类路径之后)之间的区别。
Spring classpath prefix difference
为了清楚起见,我所做的只是将两个di.xml文件"移动"到"其他" Gradle/module-subproject。我没有使用我在最初的问题中展示的任何"myCoolConfigurationName"内容,作为我的"尝试"之一。
"如此简单"......我很高兴,也不相信我与此斗争的日子。

相关问题