创建工件叠加层

时间:2014-03-03 19:47:20

标签: java maven overlay pom.xml

我想覆盖另一个人的项目,使用特定文件的自定义(即一个文件将使用不同的控制器,或以一种方式显示某些内容,或具有不同的进程)。我们一直在使用覆盖,它覆盖了我的存储库中的所有文件,并将所有文件覆盖在他们的文件之上,这样我的较小的自定义集仍然像大项目那样。到目前为止,我们已经完成了它,但它对IDE造成了严重破坏。有没有更好的办法?我尝试使用maven-builder-helper-plugin,但它给了我重写的java文件重复的类。

<build>
   <sourceDirectory>target/overlay</sourceDirectory>
      <resources>
        <resource>
          <directory>target/overlay</directory>
          <filtering>false</filtering>
          <excludes>
            <exclude>**/*.java</exclude>
          </excludes>
        </resource>
      </resources>
      <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <phase>generate-sources</phase>
                <configuration>
                <tasks>
                    <copy todir="target/overlay" overwrite="true">
                        <fileset dir="src/main/java">
                        </fileset>
                        <fileset dir="src/main/resources">
                        </fileset>
                    </copy>
                </tasks>
                </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.sample.service</groupId>
                                <artifactId>sampleService</artifactId>  
                                <classifier>sources</classifier>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>target/overlay</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我应该注意到,使用与此不同的方向我绝对没问题。我只是无法想出一个更好的方法来包含所有的资源和他们需要的源文件,以保持项目正常运行。这适用于作为Web应用程序一部分的模块。正在构建的模块和它所覆盖的模块都是jar的

2 个答案:

答案 0 :(得分:0)

我们使用maven war叠加功能(read about it here)执行此操作。要做到这一点,我们只需要从我们的Web客户端模块到战争类型<type>war</type>的依赖关系,并覆盖我们模块中需要的内容。 Maven将获取依赖战争的内容并使用其文件为我们的Web客户端提供内容,但如果文件名存在冲突,它将从我们的Web客户端获取文件,而不是从依赖jar。这就好像它需要依赖的jar并覆盖我们的客户端以产生新的战争。我们这样做非常简单,不需要任何蚂蚁运行 - 在使用maven多年的时间里,我从来没有采用过antrun(我认为它是反模式)。

您在评论中提到您在处理某些xml文件时遇到了单独的问题。这可以由资源插件处理,如果你告诉它你的文件在哪个目录,并给它一个通配符来定义你需要复制的文件。

答案 1 :(得分:0)

所以,我找到了一个解决方案,允许我覆盖工件而不会弄乱源目录。我没有把我想要覆盖的所有文件都叠加在工件文件之上,而是意识到一种更简单的方法是扩展工件,删除重复的类/资源,并将其用作附加源。我创建了自己的插件,需要尽可能多的来源&#39;我想要的目录,它按照加载的顺序过滤它们。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.sample.service</groupId>
                                <artifactId>sampleService</artifactId>  
                                <classifier>sources</classifier>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>${additional-source-folder}</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>mn-stateadvantage</groupId>
            <artifactId>duplicateSourceRemover-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <goals>
                        <goal>removeDuplicates</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceDirectories>
                            <param>src/main</param>
                            <param>${additional-source-folder}</param>
                        </sourceDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${additional-source-folder}</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>${additional-source-folder}</directory>
                                <excludes>
                                    <exclude>**/*.java</exclude>
                                </excludes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>