【问题标题】:maven create zip with jar and some more filesmaven 用 jar 和更多文件创建 zip
【发布时间】:2012-01-07 02:11:47
【问题描述】:

我不懂maven。更好地使用 ant,但是......我已经成功地创建了 jar(有或没有依赖项),我已经设法将 bat runner 脚本复制到 jar 附近,但现在我想用这个 jar 和这个 bat 创建 zip。所以我使用程序集插件并得到 BUUUM !!!卡达姆!在我的配置中,它与 jar 打包并行执行。我写了汇编文件:

    <assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>jg</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/123</outputDirectory>
            <excludes>
                <exclude>assembly/**</exclude>
                <exclude>runners/**</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

然后,我绑定了maven-assembly-plugin:

<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <inherited>false</inherited>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>by.dev.madhead.lzwj.Main</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                    <descriptors>
                            <descriptor>src/main/resources/assembly/assembly.xml</descriptor>
                            <!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>

现在我在./target

  1. runner.bat
  2. jar_without_dependencies.jar(它来自 maven-jar-plugin,对吧?)
  3. jar_without_dependencies.jar

第三个让我很生气。它包含: 而123目录包含:

如您所见,我得到了带有解压依赖项的 jar,EXCLUDED DIRS!!!! 和 dir 123,这实际上是我想要的(哦!程序集插件做到了!!!)。

我想获取带有依赖项的 jar 并使用类路径正确清单。作为一个选项,我想要带有解压依赖项的 jar(我知道 &lt;unpack&gt;false&lt;/unpack&gt; 在程序集中,但无法让它工作)。我想将 /123 更改为 / 并获得没有排除文件的普通 JAR !!!我想要两个单独的任务来构建 jar 和 zip(它是用 maven 中的配置文件完成的吗??)就像在 ant 中一样,我会写这样的东西:

    <target name="jar_with_deps" depends-on="compile">
        <jar>
            here i copy classes (excluding some dirs with runner script), and build manifest
        </jar>
        <copy>
            copy bat file from src/main/resources/runner/runner.bat
        </copy>
    </target>
    <target name="zip" depends-on="jar_with_deps">
        <zip>
            Get jar from previous target, get runner.bat. Put them in zip
        </zip>
    </target>

对不起,如果我表达得太过分了,但我对这种含蓄的行为真的很生气。我真的被这个困住了。

【问题讨论】:

  • jar-with-dependencies 也不起作用
  • 试图删除 jar。不起作用,因为 jar 是默认包装...
  • 好的,jar-with-dependencies 工作了。但仍需要对其进行自定义。
  • 与其把你所做的事情写得一团糟,不如写出你想要达到的目标。我很想帮忙,但在第一段之后我就停止阅读了,因为你写的离题太多了。
  • 我想要两个单独的“任务”:创建带有依赖项的 jar 并使用它生成 zip + 来自资源的 bat 文件。第二个“任务”必须依赖于第一个。

标签: maven maven-3 maven-assembly-plugin maven-jar-plugin


【解决方案1】:

以防万一它对其他人有所帮助,我发现这很容易做到,至少对于我的基本需求而言。我已经在使用 Maven Shade 插件来构建一个包含所有依赖项的 jar:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.1</version>
  <configuration></configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
    </execution>
  </executions>
</plugin>

所以当我运行 mvn package 时,它会生成 target/MyApp-version.jar,而我想要一个包含 MyApp-version.jar 以及其他一些文件(自述文件等)的 MyApp-version.zip。所以,我添加了 Assembly 插件:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.5.5</version>
  <configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptors>
      <descriptor>assembly.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

上面的block引用了assembly.xml,它配置了the way the plugin works

<?xml version="1.0" encoding="utf-8"?>
<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">
    <id>release</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>target</directory>
            <includes>
                <include>MyApp-${app.version}.jar</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>CHANGES.md</source>
            <fileMode>0644</fileMode>
        </file>
        <file>
            <source>LICENSE</source>
            <fileMode>0644</fileMode>
        </file>
        <file>
            <source>README</source>
            <fileMode>0644</fileMode>
        </file>
    </files>
</assembly>

${app.version} 在 pom.xml 中定义 &lt;properties&gt; 元素。)

就是这样,现在mvn package 会生成 jar 和 zip。

【讨论】:

    【解决方案2】:

    我在 pom.xml 中创建了两个配置文件:

    <profiles>
        <profile>
            <id>jar-with-dependencies</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.2.1</version>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>by.dev.madhead.lzwj.Main</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>distro</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.2.1</version>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/assembly/distro.xml</descriptor>
                            </descriptors>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    现在我可以创建简单的 jar (mvn clean package)、带有依赖项的 jar (mvn clean package -Pjar-with-dependencies)。我也可以致电mvn package -Pdistro 来创建 zip。但我需要在手动调用之前使用 -Pjar-with-dependencies 调用 maven。除此之外,一切正常。

    【讨论】:

      【解决方案3】:

      你必须选择实现你的目标:

      1. 选项:创建两个程序集描述符,一个用于 jar w/deps,一个用于 zip。 Zip 采用新创建的 jar。
      2. 选项:在您的项目中再创建两个模块:第一个模块 shades 全部放入一个 jar 中(或将一个带阴影的 jar 与您的主 jar 一起附加,保存另一个模块),让第二个模块依赖它并吸入你程序集中的那个罐子。大功告成。

      根据您项目的大小和结构,我会选择安全的方法:选项 2。保证此选项具有正确的构建和部署顺序。选项 1 在某种程度上违反了 maven 方式。

      【讨论】:

      • 当我创建两个程序集描述符时,第二个(用于 zip)找不到第一个的输出。我如何指定他们的订单?
      • 顺序和pom中定义的一样。附上第二个工件,这个罐子也包括在内。请参阅useProjectAttachments 属性。
      猜你喜欢
      • 2016-02-12
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多