我有一个正在以特定方式构建和部署的工件(不是作为 jar 文件)。在部署过程中,会构建一个war文件。
如何配置 pom 以便工件也作为 jar 文件部署到不同的位置?
Mavendeploy
意味着将工件部署到Maven 存储库,而不是应用程序服务器。
像这样配置额外的 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>
这篇博文及其评论给出了答案。
这三个插件配置将允许您在战争中构建/安装/部署 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>
“maven 方式”是拆分src/main/java
成一个单独的模块,并让 war 文件依赖于它。
如果您绝对反对这种方法,则可以使用配置文件来更改元素的内容packaging
。我不确定这是否可能。
将它们分开是正确的方法。强制 maven 在同一个模块中产生一个战争和一个 jar 是可能的,但会导致你在路上遇到问题。
您应该在 pom 文件的依赖项中添加工件的相应依赖项。
前任:
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
解决此问题的一种方法是让模块构建一个 jar,然后使用程序集插件在该 war 的 WEB-INF/lib 中使用该 jar 构建一个 war 文件。我强烈建议不要这样做。最好有一个 jar 项目和一个带有构建两个模块的父项目的 war 项目。