8

我不懂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_dependencies.jar (它来自 maven-jar-plugin,对吧?)
  3. jar_without_dependencies.jar

第三个激怒了我。它包含:在此处输入图像描述 并且 123 目录包含:在此处输入图像描述

如您所见,我得到了带有解压缩依赖项的 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>

对不起,如果我表达得太过分了,但我对这种含蓄的行为真的很生气。我真的坚持这一点。

4

3 回答 3

9

以防万一它对其他人有所帮助,我发现这很容易做到,至少对于我的基本需求而言。我已经在使用 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,它配置了插件的工作方式

<?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生成 jar 和 zip。

于 2015-10-01T14:54:52.330 回答
1

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

  1. 选项:创建两个程序集描述符,一个用于 jar w/deps,一个用于 zip。Zip 采用新创建的 jar。
  2. 选项:在您的项目中再创建两个模块:第一个模块将所有 deps 隐藏到一个 jar 中(或将一个带阴影的 jar 与您的主 jar 一起附加,保存另一个模块),让第二个模块依赖它并将该 jar 吸入您的程序集中. 你完成了。

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

于 2011-11-26T23:15:24.490 回答
1

我在 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)、带有依赖项 ( mvn clean package -Pjar-with-dependencies) 的 jar。我也可以调用mvn package -Pdistro创建 zip。但我需要在手动调用之前使用 -Pjar-with-dependencies 调用 maven。除此以外,一切正常。

于 2011-11-27T09:40:55.233 回答