简体   繁体   English

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

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

i have a jar say xyz.jar which has structure as src/main/java/resources . 我有一个罐子说xyz.jar,其结构为src / main / java / resources。 Now this resources folder has 3 sub-folders say a/fileone.txt b/filetwo.txt and c/filethree.txt . 现在,此资源文件夹具有3个子文件夹,分别是a / fileone.txt b / filetwo.txt和c / filethree.txt。 I am using this jar as a dependency for building three 3 different war files. 我使用此jar作为构建三个3个不同war文件的依赖项。 In each of these war files i use only one of the 3 files .ie either fileone.txt or filetwo.txt or filethree.txt. 在这些战争文件的每一个中,我仅使用3个文件之一,即fileone.txt或filetwo.txt或filethree.txt。 So in the pom.xml for building any of the 3 war files, is there any way by which i can configure to exclude the remaining two files? 因此,在用于构建3个war文件中的任何一个的pom.xml中,是否可以通过任何方式配置为排除其余两个文件? For eg if i am building firstWar.war i want to include only fileone.txt and exclude the other two. 例如,如果我要构建firstWar.war,我只想包含fileone.txt而排除其他两个。 I believe that packageExcludes in maven war plugin can be used here but am not sure how? 我相信可以在这里使用maven war插件中的packageExcludes ,但不确定如何? Thanks. 谢谢。

  • Solution 1: 解决方案1:

You are assuming you have jar file which contains the resources. 您假设您有包含资源的jar文件。 I would suggest to put the files/resources into the war module instead and produce three different war's from a single build. 我建议将文件/资源​​放到war模块中,并从一个构建中产生三个不同的war。 This can be solved by using the maven-assembly-plugin. 这可以通过使用maven-assembly-plugin解决。 You have the following structure: 您具有以下结构:

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

You need an assembly-descriptor and of course a pom file like this: 您需要一个汇编描述符,当然也需要一个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>

The descriptor file looks like this: 描述符文件如下所示:

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

Which you need for every resource (in your case three times). 每个资源所需的资源(在您的情况下为3次)。 They can be named like your environments like test, qa, production (don't forget to give them an appropriate id). 可以像测试,质量检查,生产这样的环境来命名它们(别忘了给它们提供适当的ID)。 They should be placed into src/main/assembly folder. 它们应该放在src / main / assembly文件夹中。 Or in relationship to your envrionment (file1, file2, file3 but i think there exist better names in reality.). 或与您的环境有关(file1,file2,file3,但我认为现实中存在更好的名称。)。

  • Solution 2: 解决方案2:

You will do the same setup for the jar file which you use and create three different jar files with an appropriate classifier which represents the resource you like. 您将对所使用的jar文件进行相同的设置,并使用代表您喜欢的资源的适当分类器创建三个不同的jar文件。 But afterwards you have to change the war build to create three different war file for every resources different one. 但是之后,您必须更改战争版本,以便为每个不同的资源创建三个不同的战争文件。 About the setup i wrote a blog entry . 关于设置, 我写了一个博客文章

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

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