Java 如何在多模块web项目中部署WAR中的资源
好的,我有一个多模块的web项目(Spring、Hibernate等等),由一个WAR和3个JAR组成。其中2个JAR是核心WAR和JAR的可选扩展。这些扩展由maven概要文件使用,maven概要文件将它们作为依赖项添加或不添加。这一切都很好 然而,在Tomcat服务器上安装项目时,我注意到我无法编辑扩展模块的属性文件,因为它们打包在JAR文件中。因此,我正在寻找一种方法,在使用maven打包项目时,将属性文件从扩展JAR移到可部署WAR的resources文件夹中。这意味着,只有在激活扩展时,资源才会位于WEB资源文件夹中 我怎样才能做到最好 EDIT-01 好的,我已经尝试使用qza建议的build helper maven插件 测试战争POM:Java 如何在多模块web项目中部署WAR中的资源,java,spring,hibernate,maven,tomcat,Java,Spring,Hibernate,Maven,Tomcat,好的,我有一个多模块的web项目(Spring、Hibernate等等),由一个WAR和3个JAR组成。其中2个JAR是核心WAR和JAR的可选扩展。这些扩展由maven概要文件使用,maven概要文件将它们作为依赖项添加或不添加。这一切都很好 然而,在Tomcat服务器上安装项目时,我注意到我无法编辑扩展模块的属性文件,因为它们打包在JAR文件中。因此,我正在寻找一种方法,在使用maven打包项目时,将属性文件从扩展JAR移到可部署WAR的resources文件夹中。这意味着,只有在激活扩展时
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-resource</id>
<phase>process-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../extTestA-jar/src/main/resources</directory>
<targetPath>resources</targetPath>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
...
org.apache.maven.plugins
maven战争插件
2.1.1
假的
org.codehaus.mojo
构建助手maven插件
1.8
添加资源
过程资源
添加资源
../extTestA jar/src/main/resources
资源
我的项目布局如下:
<Parent>
--><extTestA-jar>
--><extTestB-jar>
--><test-jar>
--><test-war>
-->
-->
-->
-->
使用“src/main/resources”中的我的资源
有人能发现这个问题吗?我试过使用和不使用targetPath,但似乎都不起作用。我也尝试过在生成资源阶段这样做,但仍然没有成功。我认为路径可能是错误的,所以我也尝试了其他方法,但仍然没有成功。最好从项目外部阅读属性。为应用程序服务器创建一个配置文件夹,并在服务器启动脚本中定义它,例如:
-Dconfiguration.dir=/etc/tomcatConf
然后将配置文件/文件夹放在该文件夹下,并使用spring属性占位符bean扫描这些文件/文件夹:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${configuration.dir}/module1/auth.properties</value>
</list>
</property>
</bean>
文件:${configuration.dir}/module1/auth.properties
现在,当您更改属性时,只需重新启动服务器,不需要重新创建war/jard
请注意,您不需要有系统参数。它用于为应用程序服务器保留标准配置目录。您只需使用:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:/etc/conf/auth.properties</value>
</list>
</property>
</bean>
文件:/etc/conf/auth.properties
一种解决方案是使用并执行目标:
添加资源
完整的例子是:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-resources</id>
<phase>process-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../data-module/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
org.codehaus.mojo
和目标
复制资源
对于该插件,用法与此类似:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/resources</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>../data-module/src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
maven资源插件
复制资源
产生资源
复制资源
src/main/resources
真的
../data module/src/main/resources
*.物业
这两个插件都可以帮助完成像这样的任务。添加资源看起来很有希望。我会试试的。谢谢完成后将发布结果。好的,我已经尝试了添加资源,但似乎无法使其工作。我没有收到任何错误,但它似乎没有任何作用。我用我的改变和我尝试过的东西更新了我原来的帖子。您看到问题了吗?生成日志显示添加资源目标的执行情况。可能会出现类似“跳过不存在的资源目录”的信息或其他一些错误消息,如果您打算在Tomcat服务器中就地编辑属性文件,那么将它们完全外部化到webapp之外就是一种方法(如Mustafa Genc所述)。如果它们在webapp中,那么当一个新版本部署到Tomcat时,它们可能会被覆盖。但是,由于覆盖不是一个真正的问题,我的老板希望它在WAR资源文件夹中。所以我同意qza的建议。也就是说,如果两种选择都对你开放,那么穆斯塔法的答案会更好。然而,我受到老板愿望的限制。那么,有人能在我的代码中发现问题吗?这里有一个小演示,演示如何复制资源,好的,谢谢!我让它工作了。“添加资源”无法工作,因为我必须在其中添加pluginManager标记,这是一个奇怪的错误。所以我现在让他们两个都在工作。