简体   繁体   中英

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 . Now this resources folder has 3 sub-folders say a/fileone.txt b/filetwo.txt and c/filethree.txt . I am using this jar as a dependency for building three 3 different war files. In each of these war files i use only one of the 3 files .ie either fileone.txt or filetwo.txt or 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? For eg if i am building firstWar.war i want to include only fileone.txt and exclude the other two. I believe that packageExcludes in maven war plugin can be used here but am not sure how? Thanks.

  • Solution 1:

You are assuming you have jar file which contains the resources. I would suggest to put the files/resources into the war module instead and produce three different war's from a single build. This can be solved by using the 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:

 <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). They can be named like your environments like test, qa, production (don't forget to give them an appropriate id). They should be placed into src/main/assembly folder. Or in relationship to your envrionment (file1, file2, file3 but i think there exist better names in reality.).

  • Solution 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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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