【问题标题】:Adding Additional classes in maven WAR build在 Maven WAR 构建中添加其他类
【发布时间】:2013-01-21 18:53:20
【问题描述】:

我目前正在使用带有https://maven.apache.org/plugins/maven-war-plugin/ 的maven3 和带有http://mojo.codehaus.org/build-helper-maven-plugin/http://tomcat.apache.org/maven-plugin-2.0/src/main/java 旁边添加其他来源,例如src/mock/java

在运行mvn tomcat7:run 时,这些附加类以及测试资源都存在。 捆绑 WAR(通过mvn package)时,这些虚假资源被排除在外。 在大多数情况下这很好,因为战争包是我们发布的并且正在部署在产品服务器上。

问题 1: 但是:“假”类仍然在 WAR 构建中被授权,这对于生产性 WAR 来说是不干净的。

但还有另一个用例: 使用这些额外的类和资源构建一个 WAR 文件,以便通过持续集成/部署 (jenkins) 在本地开发服务器上进行部署 这似乎很棘手......

问题2:当前的WAR有fake classes但没有fake-resources ;/

问题:如何在正常构建中排除假类,但如何在 WAR 构建中包含这些来源以及假资源?

这是我的工作:

<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>

这个问题与:

【问题讨论】:

  • 也许像战争之类的东西会有所帮助?

标签: maven maven-3 war


【解决方案1】:

您应该使用不同的配置文件(请参阅http://maven.apache.org/guides/introduction/introduction-to-profiles.html)来构建您的战争。包含您的虚假来源/资源的“持续集成”配置文件,以及没有它们的“生产”配置文件。

【讨论】:

  • 我确实使用配置文件,这不是问题。问题是必须为每个用例更改配置?!
  • 再一次:分析很好..我知道..但是:必须更改哪些配置以匹配上述两个用例?!换句话说:我在哪里犯了导致问题 1 的错误?
  • 刚刚测试了“重新声明”,因为到目前为止我只使用属性来通过配置文件进行切换。注意:您不能重新声明插件,而只能重新声明配置文件中的依赖项。所以你不能使用不同的 ;/
  • 不,您可以在配置文件中声明您的插件:/。因此,您可以为每个配置文件配置或拥有不同的插件。
  • 好的,知道了。谢谢。在xsd中没有看到这个。尽管如此..核心问题仍未解决;)
【解决方案2】:

我是如何修复它的:

--- 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>

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    相关资源
    最近更新 更多