java - 在 Maven 中部署用于生产的 jar

标签 java maven deployment dependencies copy

我的产品中有多个 Java 项目(打包到 jar 中),它们有一个将它们组合为模块的 pom。每个项目都有不同的依赖项,它们的组合依赖项在父 pom 中定义。我想准备生产输出并执行以下操作:

  1. 将我的所有 jar 部署(复制)到一个位置
  2. 将所有项目中的第 3 方 jar 复制到不同的单个文件夹
  3. 将所有项目的配置文件(位于 src/main/resources 下)复制到第三个文件夹。

有人知道一种方法可以做到这一点,而不必为所有项目手动复制上述每个内容吗?我还希望我 future 的项目能够支持此部署过程,而不必过于努力和具体。

最佳答案

为此,您需要使用 Maven 程序集插件 - 以下是压缩分发的示例(在生产环境中,您需要解压缩):

  <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>${project.basedir}/src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

和汇编 XML

 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>${env}</id>
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <useProjectArtifact>false</useProjectArtifact>
        <includes>
            <include>com.mycompany:ear-project</include>
            <include>com.mycompany:jnlp-project</include>
        </includes>
        <outputDirectory>libs</outputDirectory>
    </dependencySet>
</dependencySets>

<fileSets>
    <fileSet>
        <directory>src/main/resources</directory>
        <outputDirectory>/</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>target/docbook/pdf</directory>
        <includes>
            <include>index.pdf</include>
        </includes>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

请注意,对于您的 2 个项目(EAR、JNLP),您可以通过 outputDirectory 配置放置依赖项。

关于java - 在 Maven 中部署用于生产的 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11754119/

相关文章:

java - while循环与mysql java数据库

java - 使用 Play Java 从 Firebase 接收 Json

Xcode 部署目标设置

java - Play Framework 部署失败

java - 在 AWS 上部署 Java Web 应用程序 + MongoDB

java - 错误 SQL 错误 : 1452, SQLState : 23000, 使用 hibernate 和 javafx

java - Android:键盘未显示在焦点上

java - Spring Cloud Contract 测试适用于 Maven,但不适用于 JUnit 运行

java - Maven发布插件: perform a few deploy in a row

java - 在 Java 跨平台桌面应用程序上接收推送通知