Java 是否可以让“mvn tomcat6:run”与经过筛选的web.xml一起正常工作?
我有一个项目,需要将Java 是否可以让“mvn tomcat6:run”与经过筛选的web.xml一起正常工作?,java,maven,tomcat,Java,Maven,Tomcat,我有一个项目,需要将${}占位符替换应用到web.xml,我希望使用mvn tomcat6:run运行该项目,以实现开发模式。有可能设置这个吗 这是一个遗留代码库,我试图在其中引入两种部署环境,以摆脱以前的过程,即“复制web.xml变体并编辑配置文件,并使其脱离源代码控制”。我还希望避免维护Tomcat安装和设置IDE来部署它,只需使用tomcat6 maven插件执行应用程序即可 到目前为止,我得到的是这个项目布局: foo │ pom.xml │ └───src └───ma
${}
占位符替换应用到web.xml
,我希望使用mvn tomcat6:run运行该项目,以实现开发模式。有可能设置这个吗
这是一个遗留代码库,我试图在其中引入两种部署环境,以摆脱以前的过程,即“复制web.xml变体并编辑配置文件,并使其脱离源代码控制”。我还希望避免维护Tomcat安装和设置IDE来部署它,只需使用tomcat6 maven插件执行应用程序即可
到目前为止,我得到的是这个项目布局:
foo
│ pom.xml
│
└───src
└───main
├───environments
│ ├───BAR
│ │ filter.properties
│ │
│ └───FOO
│ filter.properties
│
├───java
│ └───se
│ └───millimoo
│ IndexServlet.java
│
└───webapp
└───WEB-INF
web.xml
pom.xml
BAR\filter.properties
web.xml
。。。
指数
se.millimoo.IndexServlet
环境
${filter.environment}
指数
/
...
IndexServlet.java
打印环境
init参数的值
当我使用mvntomcat6:run
运行它时,它使用了未过滤的web.xml
,这显然不是我想要的。当我使用mvn tomcat6:run war
时,会使用过滤版本,但我不喜欢这个版本:它比较慢,并且在我的实际代码中有其他问题,通常会减慢周转,这就是练习的重点。(它使用后台代码来自Maven存储库的依赖项目的JAR文件来构建JAR,JAR可能已经过时,或者根本不可用,如果我在版本号调整后没有安装它们,而不是使用当前代码)
有没有一种方法可以在这里两全其美?是否能够通过maven或其他易于嵌入的方式运行应用程序服务器,同时对web.xml应用过滤?最好在不显著更改应用程序本身的情况下,也就是说,我不想在不使用servlet init参数的情况下加载配置值。(在真正的代码中,servlet是第三方的。)我认为您需要将其添加到pom.xml
中,以便Maven知道他也需要过滤此目录:
<build>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
src/main/webapp/WEB-INF
真的
...
编辑
正如评论中所述,这种方法可能不是一个好的解决方案。
另一个解决方案:
- 从
覆盖默认目标目录
默认情况下,web资源被复制到WAR的根目录,如图所示
在前面的示例中。覆盖默认目标的步骤
目录,指定目标路径
...
<configuration>
<webResources>
<resource>
...
</resource>
<resource>
<directory>configurations</directory>
<!-- override the destination directory for this resource -->
<targetPath>WEB-INF</targetPath>
<!-- enable filtering -->
<filtering>true</filtering>
<excludes>
<exclude>**/properties</exclude>
</excludes>
</resource>
</webResources>
</configuration>
...
。。。
...
配置
WEB-INF
真的
**/性质
...
在本例中,我们覆盖了一个目录,但可能覆盖了一个文件。我想要的目标似乎是tomcat6:runwaronly
,它在target/
中组装的webapp上启动一个Tomcat。但是,这个目标在单独运行时不会调用生命周期的其余部分,这意味着应用程序可能无法组装或更新。我首先在生命周期的包
阶段将war:war
目标替换为war:war
,并将tomcat6:run war only
绑定到下一个生命周期阶段verify
。(非常方便的是,它没有为war
项目绑定任何目标,在语义上似乎是合适的。)为了保持这一点,我将其放在一个概要文件中:
<profile>
<id>embedded</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- Replace running war:war with war:exploded in the lifecycle -->
<execution>
<id>embedded-war</id>
<goals>
<goal>exploded</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>default-war</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- Run Tomcat over the assembled webapp -->
<execution>
<id>embedded-verify</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
它的一个缺陷是,它不会反映对视图等的实时更改,但在Tomcat服务器运行时运行mvn war:explodesd
重新组装应用程序时,应该注意这一点。在最终构建的WEB-INF/classes中包含该目录的内容,这意味着WAR将有两个目录内容的副本嵌套在另一个目录中——一个有过滤文件,一个没有。这听起来像是一种很好的方法,可以让bug变得非常混乱,使战争变得更加臃肿,使其构建更加缓慢,而且可能无法工作,因为Tomcat插件可能仍然从源目录加载web.xml。(我稍后会检查最后一部分。)但一般来说,WAR插件自己处理该目录是有原因的,而且它不包含在
我理解的目录下。正如你所说,这可能不是一个好的解决办法。我将编辑我的答案。但我不想从另一个资源目录复制WEB-INF下的任何内容,WEB.xml
已位于默认位置src\main\webapp\WEB-INF
。我尝试创建一个新目录src/main/WEB.INF.filtered
,其中包含我的WEB.xml
,使用WEB-INF
。这场战争打得很好——但它以前就这样做过。但是,tomcat6:run
找不到我的IndexServlet;这是有道理的,因为不再有src/main/webapp/WEB-INF/WEB.xml
。主要的问题似乎是Tomcat插件忽略了WAR
插件中关于web资源的任何配置。
...
<servlet>
<servlet-name>Index</servlet-name>
<servlet-class>se.millimoo.IndexServlet</servlet-class>
<init-param>
<param-name>environment</param-name>
<param-value>${filter.environment}</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Index</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
...
<build>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
...
<configuration>
<webResources>
<resource>
...
</resource>
<resource>
<directory>configurations</directory>
<!-- override the destination directory for this resource -->
<targetPath>WEB-INF</targetPath>
<!-- enable filtering -->
<filtering>true</filtering>
<excludes>
<exclude>**/properties</exclude>
</excludes>
</resource>
</webResources>
</configuration>
...
<profile>
<id>embedded</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- Replace running war:war with war:exploded in the lifecycle -->
<execution>
<id>embedded-war</id>
<goals>
<goal>exploded</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>default-war</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- Run Tomcat over the assembled webapp -->
<execution>
<id>embedded-verify</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
mvn -P embedded,env-foo verify