maven创建带有jar和一些文件的zip

mad*_*ead 5 maven-3 maven maven-assembly-plugin maven-jar-plugin

我不明白maven.更好地使用ant,但是......我已经设法创建了jar(有或没有依赖),我已经设法将bat runner脚本复制到jar附近,但现在我想用这个jar和这个bat创建zip.所以我使用程序集插件并获取BUUUM !!!! CADAAAM!在我的配置中它发生了,它与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>
Run Code Online (Sandbox Code Playgroud)

然后,我绑定了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>
Run Code Online (Sandbox Code Playgroud)

现在我明白了./target:

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

而第三个让我感到愤怒.它包含:在此输入图像描述 并且123目录包含:在此输入图像描述

如你所见,我得到了带有解压缩依赖项的jar,排除了DIRS !!!!和dir 123,这实际上就是我想要的(哦!汇编插件就是这样!!!).

我想获得带有依赖关系的jar并使用classpath更正清单.作为一个选项,我希望jar具有解压缩的依赖关系(我<unpack>false</unpack>在汇编时知道,但无法使其工作).我想将/ 123更改为/并获取NORMAL 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>
Run Code Online (Sandbox Code Playgroud)

对不起,如果我过于表达,但我对这种隐含行为感到非常生气.我真的很困惑.

ale*_*ntd 6

为了防止其他人,我发现这很容易做到,至少对我的基本需求而言.我已经在使用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>
Run Code Online (Sandbox Code Playgroud)

因此,当我运行时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>
Run Code Online (Sandbox Code Playgroud)

上面的块指的是assembly.xml,它配置插件的工作方式:

<?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>
Run Code Online (Sandbox Code Playgroud)

(${app.version}在pom.xml <properties>元素中定义.)

就是这样,现在mvn package生产罐子和拉链.


Mic*_*l-O 1

您有两种选择来实现您的目标:

  1. 选项:创建两个程序集描述符,一个用于具有依赖项的 jar,另一个用于 zip。Zip 获取新创建的 jar。

  2. 选项:在项目中再创建两个模块:第一个模块将所有依赖项着色到一个 jar 中(或将着色的 jar 与主 jar 一起附加,保存另一个模块),让第二个模块依赖于它并将该 jar 吸入程序集中。你完成了。

根据项目的大小和结构,我会选择安全的方法:选项 2。这个方法保证具有正确的构建和依赖关系顺序。选项1有点违反了maven方式。