如何在Maven程序集中排除资源文件?

时间:2015-10-14 22:05:32

标签: java maven maven-assembly-plugin

我正在尝试从Maven构建中排除资源文件。我复制了my-jar-with-dependencies.xml并添加了一个部分。这正确地排除了将命名文件复制到jar的/ src / main / resources ,但是该文件仍然被复制到jar的顶级目录以及其他资源。

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <!-- TODO: a jarjar format would be better -->
  <id>my-jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>

  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory></outputDirectory>
      <useDefaultExcludes>true</useDefaultExcludes>
      <excludes>
        <exclude>**/lanchecker.properties</exclude>  <!-- this removes it from jar/src/main/resources but *NOT* from jar/ -->
      </excludes>
    </fileSet>
  </fileSets>

  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

我可以阻止maven-assembly-plugin执行该副本,但仍然可以在开发中访问该文件吗?

更新

依赖性,请求:

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>1.4.01</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.47.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.4</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.23</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>       

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.3</version>
    </dependency>

  </dependencies>

1 个答案:

答案 0 :(得分:3)

试试这个:

...
<dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
      <unpackOptions>
        <excludes>
          <exclude>**/lanchecker.properties</exclude>
        </excludes> 
      </unpackOptions>
    </dependencySet>
  </dependencySets>
...

这样做的原因是不同的装配选择器(<dependencySets><moduleSets><fileSets><files>针对不同的东西,它们可以重叠。

例如,<fileSets>仅定位模块中的文件(程序集绑定到的模块)。由于您指定了<useProjectArtifact>true</useProjectArtifact>,因此依赖关系集将模块作为依赖项包含在内,这就是lanchecker.properties进入程序集的原因,这就是为什么必须将其排除在外来自依赖集。

在不知道您的POM是什么样的或您想要实现的目标的情况下,指定<useProjectArtifact>false</useProjectArtifact>并仅使用<fileSets>来控制此内容就足够了模块。

另一方面,由于模块已包含在依赖关系集中(通过<useProjectArtifact>true</useProjectArtifact>,您可能只需完全删除<fileSets>部分

程序集通常是Maven用户学习掌握的最后一件事。它功能强大,灵活且复杂,特别是如果你在混合中投入多个模块,但它也可能有点古怪。