gpt4 book ai didi

maven - 如何在多模块 Maven 项目中使用 Maven 程序集插件

转载 作者:行者123 更新时间:2023-12-02 05:00:14 34 4
gpt4 key购买 nike

我是 Maven 新手,花了大约 3 天的时间使用程序集插件生成 zip 文件,引用 http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/我的项目是多模块的,所以我也引用了Managing multi-module dependencies with Maven assembly plugin

我仍然有一些效率低下的地方。下面是 assembly.xml (我从第一个链接继承)

 <assembly>
<id>bin</id>
<!-- Generates a zip package containing the needed files -->
<formats>
<format>zip</format>
</formats>

<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!-- Project artifact is not copied under library directory since it is
added to the root directory of the zip package. -->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>

</dependencySet>
</dependencySets>
<moduleSets>
<moduleSet>

<!-- Enable access to all projects in the current multimodule build! <useAllReactorProjects>true</useAllReactorProjects> -->

<!-- Now, select which projects to include in this module-set. -->
<includes>
<include>com.XX:YY-main</include>
</includes>

<!--<binaries> <outputDirectory>YY-main/target</outputDirectory> <unpack>false</unpack>
</binaries> -->
</moduleSet>
</moduleSets>
<fileSets>
<!-- Adds startup scripts to the root directory of zip package. The startup
scripts are located to src/main/scripts directory as stated by Maven conventions. -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>log4j.properties</include>
</includes>
</fileSet>
<!-- adds jar package to the root directory of zip package -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*with-dependencies.jar</include>
</includes>
</fileSet>
</fileSets>

下面是 pom.xml(主要或主要)

 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration> <descriptors>
<descriptor>YY-main/src/main/assembly/assembly.xml</descriptor>
<!-- <descriptor>DummyAssembly.xml</descriptor> -->

</descriptors>
</configuration>
</plugin>

问题:

1)由于在主 pom 中引用了 assembly.xml,因此所有子模块也会被压缩。如何仅指定要压缩的某些子模块,而忽略所有其他子模块。我尝试将 YY-main/src/main/assembly/assembly.xml 从主 pom 移动到子/子模块 pom 并在 main 中有 dummy assembly.xml ,但它什么也不做。但这不起作用(可能是因为 dummy assembly.xml 缺少一些必需的行)。尝试了一些东西,但似乎没有任何效果

2)还在 assembly.xml (依赖集)中,它将所有库复制到“lib”文件夹。我怎样才能避免这种情况。我再次尝试了一些基于 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet 的事情(排除..)但没有任何效果。

有人可以向我提供一些具体的语句,我应该在我的 pom 或程序集文件中更改这些语句以解决 1 和 2 - 谢谢

最佳答案

您应该更改的基本内容是创建一个单独的模块,在其中进行打包,如下所示。

   +-- root (pom.xml)
+--- mod-1 (pom.xml)
+--- mod-2 (pom.xml)
+--- mod-assembly (pom.xml)

mod- assembly 中的 pom 看起来像这样:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.test.parent</groupId>
<artifactId>root</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>dist</artifactId>
<packaging>pom</packaging>

<name>Packaging Test : Distribution</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-one</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-two</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-bundles</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>proj1-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

这将解决在每个子进程中运行 maven- assembly-plugin 的问题以及虚拟描述符的问题。 Here you can find a real这种结构的示例。

此外,将模块放在一个文件夹中并将依赖项放在另一文件夹中,您可以使用 assembly descriptor like this :

  <id>bin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<excludes>
<exclude>${project.groupId}:*:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<outputDirectory>modules</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>

关于maven - 如何在多模块 Maven 项目中使用 Maven 程序集插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072384/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com