2

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:

Community
  • 1
  • 1
childno͡.de
  • 4,679
  • 4
  • 31
  • 57

2 Answers2

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.

willome
  • 3,062
  • 19
  • 32
  • I DO use profiles, that's not the problem. The problem is where the config has to be changed for each usecase?! – childno͡.de Feb 06 '13 at 14:54
  • once again: profiling is fine.. I know that.. But: Which configs has to be changed to match the two use-cases described above?! In other words: Where did I make mistakes that leads to Problem 1? – childno͡.de Feb 06 '13 at 15:11
  • just tested "redeclaration" because until now I only use properties for switching things via profiles.note: you can't redeclare plugins but only dependecies in profiles. so you can't use different ;/ – childno͡.de Feb 07 '13 at 08:28
  • 1
    no you can declare your plugins inside a profile : /. So you can configure or have different plugins for each profile. – willome Feb 07 '13 at 09:52
  • ok, got it. Thanks. didn't see this in the xsd. nevertheless.. core problem still unaddressed ;) – childno͡.de Feb 13 '13 at 18:29
1

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>
childno͡.de
  • 4,679
  • 4
  • 31
  • 57