【问题标题】:Maven maven-assembly-plugin jar-with-dependencies exclude LICENSE.txt from final jarMaven maven-assembly-plugin jar-with-dependencies 从最终 jar 中排除 LICENSE.txt
【发布时间】:2014-06-17 11:46:42
【问题描述】:

我正在使用 Maven 打包单个可执行 jar,但我似乎无法弄清楚如何使它不将 LICENSE.txt 和 NOTICE.txt 文件放入其中。这些文件可以在最终的可执行文件 .jar 中找到,但我的项目目录中没有此类文件。

我尝试了多种配置,但均无效。我现在的情况如下:

在我的 pom.xml 中我有:

<build>
  <plugins>
      <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>                
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>main.Main</mainClass>
                    </manifest>
                </archive>
                <excludes>
                    <exclude>README*</exclude>
                    <exclude>LICENSE*</exclude>
                    <exclude>NOTICE*</exclude>
                </excludes>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>                    
            </configuration>
            <executions>
                <execution>
                    <id>maven-assembly-plugin</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我也尝试在 pom.xml 中使用

<descriptors>
    <descriptor>/src/assembly/src.xml</descriptor>
</descriptors>

而不是

                <excludes>
                    <exclude>README*</exclude>
                    <exclude>LICENSE*</exclude>
                    <exclude>NOTICE*</exclude>
                </excludes>

与/src/assembly/src.xml的内容:

        <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
            <id>distribution</id>
            <formats>
                <format>jar</format>
            </formats>
            <fileSets>
                <fileSet>
                    <excludes>
                        <exclude>README*</exclude>
                        <exclude>LICENSE*</exclude>
                        <exclude>NOTICE*</exclude>
                    </excludes>
                    <!--useDefaultExcludes>false</useDefaultExcludes-->
                </fileSet>
            </fileSets>
        </assembly>

但为此我得到Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.4:single (maven-assembly-plugin) on project DatabaseTaseng: Failed to create assembly: Error creating assembly archive distribution: A zip file cannot include itself -&gt; [Help 1]

我应该如何配置它以获取具有依赖关系的单个可执行 jar 而没有那些 .txt 文件?

【问题讨论】:

  • 这些文件在您的项目中的什么位置?
  • 我没有。该插件似乎在打包时创建它们或从某些依赖项中获取它们。不知道是哪一个。
  • 嗯,我不确定我是否相信你 :-) 打开文件 - 内容是什么?大概README.txt文件会包含线索。
  • 抱歉,没有 README.txt 文件,只有 LICENSE.txt 和 NOTICE.txt。 LICENSE.txt 包含 2004 年 1 月的 Apache 许可证版本 2.0 和 NOTICE.txt NOTICE file corresponding to section 4(d) of the Apache License, Version 2.0, in this case for the Apache XmlBeans distribution.。我使用 org.apache.poi.poi-ooxml 作为依赖项。是否可以将它们从中解压缩并留在存档的根目录中?
  • 是的,这很有可能。如果您删除这些文件,您可能会违反许可协议。我建议你离开他们!

标签: java maven jar


【解决方案1】:

我不使用文件集...

我使用这个配置,取自这里:

http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html

 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2   http://maven.apache.org/xsd/assembly-1.1.2.xsd">
     <id>distribution</id>
  <formats>
    <format>jar</format>
  </formats>
  <files>
    <file>
      <source>README.txt</source>
      <outputDirectory>/</outputDirectory>
      <filtered>true</filtered>
    </file>
    <file>
      <source>LICENSE.txt</source>
      <outputDirectory>/</outputDirectory>
    </file>
    <file>
      <source>NOTICE.txt</source>
      <outputDirectory>/</outputDirectory>
      <filtered>true</filtered>
    </file>
  </files>
 </assembly>

此配置应在生成输出 jar 时过滤您需要的文件...因此,它还应过滤将要生成的文件...

我希望这对你有用...

【讨论】:

    【解决方案2】:

    exclusions 标签是排除 jars 依赖。 dependencySet 标记允许unpackOptions 在解压缩依赖项时具有包含/排除文件的选项。 所以你的程序集应该是这样的:

    <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>jar-with-dependencies</id>
       <formats>
          <format>jar</format>
       </formats>
       <includeBaseDirectory>false</includeBaseDirectory>
       <dependencySets>
          <dependencySet>
             <outputDirectory>/</outputDirectory>
             <useProjectArtifact>true</useProjectArtifact>
             <unpack>true</unpack>
             <scope>runtime</scope>
             <unpackOptions>
                <excludes>
                   <exclude>**/README.txt</exclude>
                </excludes>
             </unpackOptions>
          </dependencySet>
       </dependencySets>
    </assembly>
    

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 1970-01-01
      • 2011-07-13
      • 2014-10-10
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 2014-01-08
      相关资源
      最近更新 更多