使用 Maven 部署其他 jar 文件?

2022-09-01 21:44:07

我有一个工件,它正在以特定的方式构建和部署(而不是作为jar文件)。在部署过程中,将构建一个 war 文件。

如何配置 pom,以便将工件也作为 jar 文件部署到其他位置?


答案 1

Maven 意味着将工件部署到 Maven 存储库,而不是应用程序服务器。deploy

配置其他 JAR 工件,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>make-a-jar</id>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

将此新工件附加到项目中:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
                <goal>attach-artifact</goal>
            </goals>
            <configuration>
                <artifacts>
                    <artifact>
                        <file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
                        <!-- <file>${project.build.directory}/${project.build.finalName}.jar</file> - if finalName is defined -->
                        <type>jar</type>                           
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 2

这篇博客文章及其评论有答案。

这三种插件配置将允许您在战争的同时构建/安装/部署jar版本。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>make-a-jar</id>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <packaging>jar</packaging>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>
                    ${project.build.directory}/${project.artifactId}-${project.version}.jar
                </file>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <executions>
        <execution>
            <phase>deploy</phase>
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
                <url>${project.distributionManagement.repository.url}</url>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

推荐