java - Maven 程序集——如何创建包含版本的项目 JAR,但不包含 Artifact tar.gz 文件?

标签 java maven maven-assembly-plugin

我想使用 Maven 创建 1) 当前项目的 JAR 文件,文件名中包含当前版本,myproject-version.jar,以及 2) tar.gzip 格式的包含项目 JAR 文件的整体 Artifact 以及 lib 目录中的所有依赖 JAR 和 bin 目录中的各种驱动程序脚本,但名称中没有版本号或任意 ID。我使用 assembly 插件有点工作,因为如果我使用下面的 pom.xml 和 assembly.xml,那么当我运行“mvn package”时,我可以获得一个 tar.gzip 文件,其中包含所需的 JAR 和脚本,但是我似乎无法获得正确的命名/版本控制——要么我获得项目的 JAR 文件和带有版本号的 tar.gzip 文件,要么不获得,这取决于所使用的构建/finalName。我如何分别指定这些,如果没有附加到 Artifact 名称的 ID 是不可能构建最终 Artifact 的吗?例如,我希望我的项目的 JAR 文件被命名为 myproject-0.0.1-SNAPSHOT.jar 并且整个“uber” Artifact 被命名为 myproject.tar.gz(没有版本号或附加 ID 附加到名称)。这可能吗?

我当前的 pom.xml 和 asssembly.xml 包含在下面。

pom.xml:

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>mygroup</groupId>
    <artifactId>myproject</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>myproject</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/maven/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>    
</project>

程序集.xml:

<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>bin</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <!-- the following file set includes the Python scripts/modules used to drive the monthly processing runs -->
        <fileSet>
            <directory>src/main/python</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>indices_processor.py</include>
                <include>concat_timeslices.py</include>
            </includes>
        </fileSet>
        <!-- the following file set includes the JAR artifact built by the package goal -->
        <fileSet>
            <directory>target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <!-- the following dependency set includes the dependency JARs needed by the main Java executable for indicator processing -->
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>runtime</scope>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
</assembly> 

提前感谢您的任何建议。

最佳答案

只需在程序集配置中使用 ${project.artifactId} 作为 finalName 的值。

源自您的配置的示例(注意配置中的 finalName 元素):

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/maven/assembly.xml</descriptor>
                </descriptors>
                <finalName>${project.artifactId}</finalName>
            </configuration>
            ...
        </plugin>
如果您不更改程序集的

finalName 默认为 ${project.build.finalName}${project.build.finalName} 的默认值为 ${project.artifactId}-${project.version}

关于java - Maven 程序集——如何创建包含版本的项目 JAR,但不包含 Artifact tar.gz 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978891/

相关文章:

java - maven打包的jar如何修改权限?我正在使用 maven 程序集插件

java - 将外部 jar 添加到我们的依赖项中

java - ListView弹出消息

java - 你能用Android Java写一个记录特定应用程序屏幕的应用程序吗?

java - 对主菜单的贡献

Java 正则表达式 : Negative lookahead

google-app-engine - App Engine + 模块 + Maven 的开发工作流程

java - 如何从java maven项目生成python protobuf

java - JBoss 编译但启动时出现 NoClassDefFoundError

maven-2 - 使用 Maven 创建一个 zip 文件,其中包含具有依赖关系的可执行 jar