maven 使用 jar 和其他一些文件创建 zip

2024-02-18

我不懂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. 跑步者.bat
  2. jar_without_dependency.jar(它来自 maven-jar-plugin,对吧?)
  3. jar_without_dependency.jar

And the third angers me. It contains:enter image description here And the 123 directory contains:enter image description here

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

我想要获取具有依赖项的 jar 并使用类路径正确地显示清单。作为一个选项,我想要带有解压依赖项的 jar (我知道<unpack>false</unpack>在装配中,但无法使其工作)。我想将 /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>

对不起,如果我太表达了,但我对这种含蓄的行为真的很生气。我真的被这个问题困扰了。


以防万一它对其他人有帮助,我发现这很容易做到,至少满足我的基本需求。我已经在使用 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>

上述块指的是assembly.xml,它配置插件的工作方式 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html:

<?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中定义<properties>元素。)

就是这样,现在mvn package生成罐子和拉链。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

maven 使用 jar 和其他一些文件创建 zip 的相关文章

随机推荐