Java maven 部署额外的 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/754216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:04:56  来源:igfitidea点击:

maven deploy additional jar file

javamaven-2pom.xml

提问by Joshua Swink

I have an artifact which is being built and deployed in a particular way (not as a jar file). In the course of deployment, a war file is built.

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

How can I configure the pom so that the artifact is also deployed as a jar file, to a different location?

如何配置 pom 以便工件也作为 jar 文件部署到不同的位置?

回答by Warrior

You should add the corresponding dependency of the artifact in the dependencies of the pom file.

你应该在pom文件的dependencies中添加artifact对应的依赖。

Ex:

前任:

<dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
</dependency>

回答by Dominic Mitchell

The "maven way" is to split out src/main/javainto a separate module, and have the war file depend on that.

“maven 方式”是拆分src/main/java成一个单独的模块,并让战争文件依赖于它。

If you're absolutely resistant to that approach, you may be able to use a profile to alter the contents of the packagingelement. I'm not sure if that's possible though.

如果您绝对反对这种方法,则可以使用配置文件来更改packaging元素的内容。我不确定这是否可能。

回答by Brian Fox

Separating them is the right way to go. Forcing maven to produce a war and a jar in the same module is possible but will cause you problems down the road.

将它们分开是正确的方法。强制 maven 在同一个模块中产生战争和 jar 是可能的,但会在未来给你带来问题。

回答by sal

One way to solve this is to have the module build a jar and then use the assembly plugin to build a war file with the jar in WEB-INF/lib of that war. I would strongly recommend against this. You'd be better off having a jar project and a war project with a parent project building both modules.

解决此问题的一种方法是让模块构建一个 jar,然后使用程序集插件在该战争的 WEB-INF/lib 中使用该 jar 构建一个战争文件。我强烈建议不要这样做。您最好拥有一个 jar 项目和一个带有构建这两个模块的父项目的 war 项目。

回答by yincrash

This blog postand its comments have the answer.

这篇博文及其评论给出了答案。

These three plugin configurations will allow you to build/install/deploy a jar version alongside the war.

这三个插件配置将允许您在战争的同时构建/安装/部署 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>

回答by MariuszS

Maven deploymeans deploy artifact to a Maven Repository, not an application server.

Mavendeploy意味着将工件部署到Maven Repository,而不是应用程序服务器。

Configure additional JAR artifact like this:

像这样配置额外的 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>

Attach this new artifact to your project:

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

<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>some file</file>
                        <type>jar</type>                           
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
    </executions>
</plugin>