在 Maven WAR 構建中添加其他類 (Adding Additional classes in maven WAR build)


問題描述

在 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͡.dewillomechildno͡.de)

參考文件

  1. Adding Additional classes in maven WAR build (CC BY‑SA 3.0/4.0)

#maven-3 #maven #war






相關問題

Maven <include> wildcard cocok dengan nama folder parsial (Maven <include> wildcard match on partial folder name)

Eclipse 中使用 MAVEN 3.04 的動態 Web 應用程序 (Dynamic web application in eclipse using MAVEN 3.04)

Множныя рэпазітары Maven3 (Maven3 multiple repos)

замена ўласцівасці архетыпа maven (maven archetype property substitution)

Maven 發布和版本 Maven 插件 (Maven release and versions maven plugin)

Maven-glassfish-plugin:如何指定部署目標? (Maven-glassfish-plugin: how to specify deploy target?)

在 Maven WAR 構建中添加其他類 (Adding Additional classes in maven WAR build)

分佈式 Jenkins - Linux 上的主控和 Windows 上的從屬 - 如何配置節點特定設置 (Distributed Jenkins- Master on Linux and slave on windows- How to configure node specific setting)

如何觸發Maven SCM插件根據現有目錄自動切換目標? (How to trigger Maven SCM plugin to automatically switch goals based on existing directory?)

使用 MongoDB 存儲數據的 Java 應用程序是否需要 Maven? (Is Maven necessary for a Java application that uses MongoDB to store data?)

“el-api”在 pom.xml 中什麼時候有用? (When is "el-api" useful in pom.xml?)

無法在 JDK7 上運行 Maven 3.6.3 (Can't run Maven 3.6.3 on JDK7)







留言討論