如何使用 Maven 从指定目录的内容生成 JAR 文件?

How can I use Maven to generate a JAR file from the contents of a directory I specify?

我正在使用 maven-deploy-plugin 将第三方 jar(之前从此处未显示的插件下载到我的目标目录)部署到 Nexus 作为名称 'third-party-1.0.jar',一切正常使用下面的配置。

我的目标目录中也有一个 javadoc 目录,这是这个第三方 jar 的 javadoc。我想将该 javadoc 目录打包为 'third-party-1.0-javadoc.jar'.

如果我能把目录打包成JAR,我想我可以通过下面的部署插件的javadoc参数来部署它,只是不确定如何打包自定义目录作为使用 Maven 的具有特定名称的 JAR,也许是程序集插件?

TLDR;如何使用 Maven 从指定目录的内容创建 JAR 文件?

<plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <executions>
        <execution>
            <id>default-deploy</id>
            <phase>none</phase>
        </execution>

        <execution>
             <id>deploy-jar</id>
             <phase>deploy</phase>
             <goals>
                 <goal>deploy-file</goal>
             </goals>
             <configuration>
             <file>${project.build.directory}/code.jar</file>
             <url>...</url>
             <repositoryId>...</repositoryId>  
             <url>...</url>            
             <packaging>jar</packaging>
             <generatePom>true</generatePom>
             <groupId>com.example</groupId>
             <artifactId>third-party</artifactId>
             <version>1.0</version> 
             </configuration>     
        </execution>
    </executions>
</plugin>

您可以使用 maven-jar-pluginmaven-assembly-plugin

来实现

这是通过 maven-jar-plugin

实现的方法
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <configuration>
                <classifier>docs</classifier>
                <classesDirectory>${project.build.directory}/docs</classesDirectory>
                <includes>**/*</includes>
            </configuration>
            <id>pack-docs</id>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

现在运行:

mvn clean package

它将创建 artifact_name-1.0.0-docs.jar jar

How to create addition jar using Maven?

我最终使用了 maven-assembly-plugin 而不是 maven-jar-plugin,因为它允许对生成的 JAR 进行更多控制,包括不向其添加 pom.xml,不将其附加到项目作为要部署的工件(attach=false),以及它是如何部署的 被命名。这允许 maven-deploy-plugin 控制它的部署方式,以及部署时使用的 Maven 坐标。

然后我可以使用 javadoc 标签将使用程序集插件构建的 Javadoc JAR 附加到我的部署文件执行中:

src/main/assembly/assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>package-third-party-javadoc</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/dir</directory>  
            <outputDirectory>.</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <id>package-third-party-javadoc</id>
            <phase>compile</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
                <finalName>third-party-javadoc</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <attach>false</attach>  
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <executions>
        <execution>
            <id>default-deploy</id>
            <phase>none</phase>
        </execution>

        <execution>
            <id>deploy-jar</id>
            <phase>deploy</phase>
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
                <file>${project.build.directory}/code.jar</file>
                <url>...</url>
                <repositoryId>...</repositoryId>  
                <url>...</url>            
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
                <groupId>com.example</groupId>
                <artifactId>third-party</artifactId>
                <version>1.0</version> 
                <javadoc>${project.build.directory}/third-party-javadoc.jar</javadoc>
            </configuration>     
        </execution>
    </executions>
</plugin>