0

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

4

1 回答 1

1
  • 解决方案1:

您假设您有包含资源的 jar 文件。我建议将文件/资源​​放入战争模块中,并从单个构建中生成三个不同的战争。这可以通过使用 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>

每个资源都需要它(在你的情况下是三次)。它们可以像您的环境一样命名,例如测试、质量保证、生产环境(不要忘记给它们一个适当的 ID)。它们应该放在 src/main/assembly 文件夹中。或与您的环境有关(file1、file2、file3,但我认为现实中存在更好的名称。)。

  • 解决方案2:

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

于 2012-04-29T14:55:39.500 回答