問題描述
在 Maven WAR 構建中添加其他類 (Adding Additional classes in maven WAR build)
I'm currently using maven3 with https://maven.apache.org/plugins/maven‑war‑plugin/ and http://tomcat.apache.org/maven‑plugin‑2.0/ with http://mojo.codehaus.org/build‑helper‑maven‑plugin/ to add additional sources next to src/main/java
like src/mock/java
When running mvn tomcat7:run
these additional classes but also the test resources are present. When bundling the WAR (via mvn package
), these fake‑resources are excluded. That is fine in most cases because the war bundle is what we ship and is beeing deployed on prod server.
Problem 1: BUT: The "fake" classes are still entitled in the WAR build what is not clean for productive WARs.
But there is another usecase: Building a WAR file WITH these additional classes AND resources for deploying on a local dev server via Continuous Integration / Deployment (jenkins) That's seems to be tricky...
Problem 2: The current WAR has the fake classes but not the fake‑resources ;/
Question: How to EXclude the fake classes in normal build but how to INCLUDE these sources and also the fake resources in WAR build?
here is what I do:
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/mock/resources</directory>
</testResource>
</testResources>
…
… // plugins section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven‑resources‑plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy‑resources‑after‑test</id>
<phase>prepare‑package</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build‑helper‑maven‑plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add‑source</id>
<phase>generate‑sources</phase>
<goals>
<goal>add‑source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/java‑fake</source>
<source>${project.basedir}/src/mock/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven‑war‑plugin</artifactId>
<version>2.2</version>
<configuration>
</configuration>
</plugin>
This question is related to:
- Maven (surefire): copy test resources from src/test/java
- maven2: excluding directory from WAR
‑‑‑‑‑
參考解法
方法 1:
You should use different profiles (see http://maven.apache.org/guides/introduction/introduction‑to‑profiles.html) to build your war. A "continuous integration" profile with your fake sources/resources, and a "production" profile without them.
方法 2:
How I fixed it atm:
‑‑‑ a/pom.xml
+++ b/pom.xml
@@ ‑20,5 +20,11 @@
<properties>
+
+ <!‑‑ remove fake data for normal builds ‑‑>
+ <maven.war.warSourceExcludes>staticFakeFiles/</maven.war.warSourceExcludes>
+ <exclude.fake.resources>**</exclude.fake.resources>
+ <!‑‑ set additional fake sources to default source directory to prevent NPE ‑‑>
+ <additional.fake‑sources>${project.basedir}/src/mock/java</additional.fake‑sources>
</properties>
<organization>
@@ ‑328,6 +334,13 @@
<exclude>application.wsdl</exclude>
</excludes>
</resource>
+ <resource>
+ <directory>src/mock/resources</directory>
+ <filtering>true</filtering>
+ <excludes>
+ <exclude>${exclude.fake.resources}</exclude>
+ </excludes>
+ </resource>
</resources>
<testResources>
<testResource>
@@ ‑382,7 +392,7 @@
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/java‑fake</source>
‑ <source>${project.basedir}/src/mock/java</source>
+ <source>${additional.fake‑sources}</source>
</sources>
</configuration>
@@ ‑410,7 +420,7 @@
<version>2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
‑ <warSourceExcludes>fakefiles/</warSourceExcludes>
+ <warSourceExcludes>${maven.war.warSourceExcludes}</warSourceExcludes>
</configuration>
</plugin>
@@ ‑1353,6 +1363,11 @@
<properties>
+
+ <!‑‑ add fake data for fake builds ‑‑>
+ <maven.war.warSourceExcludes></maven.war.warSourceExcludes>
+ <exclude.fake.resources></exclude.fake.resources>
+ <additional.fake‑sources>${project.basedir}/src/mock/java</additional.fake‑sources>
</properties>
</profile>
<profile>
(by childno͡.de、willome、childno͡.de)