【问题标题】:How to create custom zip file including dependencies using Maven如何使用 Maven 创建包含依赖项的自定义 zip 文件
【发布时间】:2020-10-27 12:03:28
【问题描述】:

我是 Maven 新手,需要创建一个“自定义”压缩包,其中包含:

  • 我的课程的罐子
  • 我用来构建的一些(但不是全部)依赖项:这些是在 Maven 中声明的(例如,这些不是项目中管理的本地副本)

所需的 ZIP 文件将具有以下结构:

  • 元信息/
    • some-custom-descriptor.xml
  • lib/
    • myLib.jar
    • dependency1.jar
    • dependency2.jar

到目前为止,我了解组装插件是交易工具,但我不知道如何执行这两个步骤:

  • 为二进制文件创建 jar
  • 将此 jar一起添加和一些特定的依赖项

【问题讨论】:

    标签: maven maven-assembly-plugin


    【解决方案1】:

    我不确定我是否理解了所有内容。 但我认为你必须先复制依赖项(之后jar里面没有)

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
               <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.demo.App</mainClass>
                    <classpathPrefix>lib/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                        <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                        <excludeScope>test</excludeScope>
                    </configuration>
                </execution>
         </executions>
    </plugin>
    

    之后,您可以使用带有描述符 xml 文件的程序集插件,如下所示:

    <assembly>
        <id>zip</id>
        <formats>
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory>${project.build.directory}/lib</directory>
                <outputDirectory>lib</outputDirectory>
            </fileSet>
            <fileSet>
                <directory>${project.build.directory}</directory>
                <outputDirectory></outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>
    

    【讨论】:

      猜你喜欢
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 2012-07-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2021-03-15
      相关资源
      最近更新 更多