繁体   English   中英

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

[英]excluding certain files from a jar which is a dependency in a war in maven3

我有一个罐子说xyz.jar,其结构为src / main / java / resources。 现在,此资源文件夹具有3个子文件夹,分别是a / fileone.txt b / filetwo.txt和c / filethree.txt。 我使用此jar作为构建三个3个不同war文件的依赖项。 在这些战争文件的每一个中,我仅使用3个文件之一,即fileone.txt或filetwo.txt或filethree.txt。 因此,在用于构建3个war文件中的任何一个的pom.xml中,是否可以通过任何方式配置为排除其余两个文件? 例如,如果我要构建firstWar.war,我只想包含fileone.txt而排除其他两个。 我相信可以在这里使用maven war插件中的packageExcludes ,但不确定如何? 谢谢。

  • 解决方案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>

每个资源所需的资源(在您的情况下为3次)。 可以像测试,质量检查,生产这样的环境来命名它们(别忘了给它们提供适当的ID)。 它们应该放在src / main / assembly文件夹中。 或与您的环境有关(file1,file2,file3,但我认为现实中存在更好的名称。)。

  • 解决方案2:

您将对所使用的jar文件进行相同的设置,并使用代表您喜欢的资源的适当分类器创建三个不同的jar文件。 但是之后,您必须更改战争版本,以便为每个不同的资源创建三个不同的战争文件。 关于设置, 我写了一个博客文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM