从jar中排除某些文件,这是maven3中的战争依赖

时间:2012-04-29 12:41:19

标签: java maven-3 war

我有一个jar说xyz.jar,其结构为src / main / java / resources。现在这个资源文件夹有3个子文件夹,例如/ fileone.txt b / filetwo.txt和c / filethree.txt。我使用这个jar作为构建三个3个不同war文件的依赖项。在每个war文件中,我只使用3个文件中的一个.i.e fileone.txt或filetwo.txt或filethree.txt。所以在用于构建3个war文件中的任何一个的pom.xml中,有什么方法可以配置为排除剩余的两个文件? 例如,如果我正在构建firstWar.war,我想只包含fileone.txt并排除其他两个。 我相信maven war插件中的packageExcludes可以在这里使用,但我不确定如何? 感谢。

1 个答案:

答案 0 :(得分:1)

  • 解决方案1:

您假设您拥有包含资源的jar文件。我建议将文件/资源​​放入war模块中,并从单个构建中生成三个不同的war。这可以通过使用maven-assembly-plugin来解决。您有以下结构:

.
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |-- resources
    |   |-- environment
    |   |   |-- test
    |   |   |   `-- database.properties
    |   |   |-- qa
    |   |   |   `-- database.properties
    |   |   `-- production
    |   |       `-- database.properties
    |   `-- webapp

你需要一个汇编描述符,当然还有一个像这样的pom文件:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <id>test</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>qa</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>production</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
  </plugin>

描述符文件如下所示:

<assembly...
   <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

您需要的每个资源(在您的情况下三次)。它们可以像您的环境一样命名为test,qa,production(不要忘记给它们一个合适的id)。它们应该放在src / main / assembly文件夹中。或者与你的环境有关(file1,file2,file3,但我认为现实中有更好的名字。)。

  • 解决方案2:

您将对您使用的jar文件执行相同的设置,并使用适当的分类器创建三个不同的jar文件,该分类器代表您喜欢的资源。但之后你必须改变战争构建,为每个不同的资源创建三个不同的战争文件。关于设置i wrote a blog entry