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

发布于 2024-12-18 10:34:50 字数 3856 浏览 3 评论 0原文

我不懂maven。更好地使用ant,但是...我已经设法创建jar(有或没有依赖项),我已经设法将bat运行程序脚本复制到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-assemble-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_dependency.jar(它来自maven-jar-plugin,对吧?)
  3. jar_without_dependency.jar

第三个让我生气。它包含:在此处输入图像描述 123 目录包含:在此处输入图像描述

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

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

对不起,如果我太富有表现力,但我对这种隐式行为真的很生气。我真的被这个问题困扰了。

I do not understand maven. Better use ant, but... I've managed to create jar (with, or without dependencies), I've managed to copy bat runner script close to jar but now i want to create zip with this jar and this bat. So i use assembly plugin and get BUUUM!!!! CADAAAM! In my configuration it happens so, that it executes parallel to jar packaging. I wrote assembly file:

    <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>

Then, I bound 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>

Now I get this in ./target:

  1. runner.bat
  2. jar_without_dependencies.jar (it is from maven-jar-plugin, right?)
  3. jar_without_dependencies.jar

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

As you see, I get jar with unpacked dependencies, EXCLUDED DIRS!!!!, and with dir 123, which is actually what I want (Oh! assembly plugin did that!!!).

I want to get jar with dependencies and correct manifest with classpath. As an option i want jar with unpacked dependencies (I know about <unpack>false</unpack> in assembly, but cannot get it work). I want to change /123 to / and get NORMAL JAR WITHOUT EXCLUDED FILES!!! I want two separate tasks to build jar and zip (is it done with profiles in maven??) As in ant, i would wrote something like this:

    <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>

Excuse me, if I am too expressive, but I am really angry with this implicit behavior. I am really stuck with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇 2024-12-25 10:34:50

以防万一它对其他人有帮助,我发现这很容易做到,至少满足我的基本需求。我已经在使用 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。包含 MyApp-version.jar 以及其他一些文件(自述文件等)的 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,它配置了 插件的工作方式:(

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

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

Just in case it helps anyone else, I found that this was pretty easy to do, at least for my basic needs. I was already using the Maven Shade plugin to build a jar with all dependencies included:

<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>

So when I ran mvn package, it would produce target/MyApp-version.jar, whereas I wanted a MyApp-version.zip containing MyApp-version.jar along with some other files (a README, etc.). So, I add the Assembly plugin:

<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>

The above block refers to assembly.xml, which configures 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} is defined in the pom.xml <properties> element.)

That's it, now mvn package produces both the jar and the zip.

方圜几里 2024-12-25 10:34:50

您有两个选项可以实现您的目标:

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

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

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

You have two options to achieve your goal:

  1. option: Create two assembly descriptors, one for jar with dependencies and one for zip. Zip takes the newly created jar.

  2. option: Create two more modules in your project: first modules shades all dependencies into one jar (or attach a shaded jar along with your main jar, save another module), have the second module depend on it and suck in that jar in your assembly. Your done.

Depending on the size and structure of your project, I would go for the safe way: option 2. This one is guaranteed to have the correct build and dependencies order. Option 1 violates the maven way somewhat.

说好的呢 2024-12-25 10:34:50

我在 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 。除了这个,一切都好。

I've made two profiles in 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>

Now i am able to create simple jar (mvn clean package), jar with dependencies (mvn clean package -Pjar-with-dependencies). I also can call mvn package -Pdistro to create zip. But i need to call maven with -Pjar-with-dependencies before it manually. Except this, everything is ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文