首页 > 解决方案 > 使用 Maven 程序集插件创建一个存档,然后将其存储在项目中,然后标记项目在 Jenkins 中进行发布

问题描述

有没有办法让 Maven 程序集插件创建一个存档,添加到项目中并在部署时包含在项目 jar 文件中?

我有一个 Maven 项目。我正在尝试通过 Jenkins 对项目进行“发布”。使用 Maven 程序集插件,当我在 Jenkins 中执行 Maven 发布时,我能够生成一个部署到 Nexus 存储库的存档。 这很好,但我也希望存档驻留在原始项目中,这样当发布执行“git tag”时,项目包含存档。 我注意到 Jenkins 在调用 Maven 程序集插件之前做了一个“git tag -F”。因此,在我目前的设置下,Jenkins 似乎无法通过 git 标签将存档存储在项目中,因为存档还不存在。

[INFO] Executing: /bin/sh -c cd /Users/me/.jenkins/workspace/MyProject && git tag -F /var/folders/2r/1234abcd/T/maven-scm-123456789.commit my-project-0.0.1

……后来……

[INFO] [INFO] maven-assembly-plugin:3.3.0:single (make-assembly) @ my-project
[INFO] [INFO] Reading assembly descriptor: src/main/assembly/assembly.xml
[INFO] [INFO] Building zip: /Users/me/.jenkins/workspace/MyProject/target/checkout/target/my-project.zip
[INFO] [INFO] Building tar: /Users/me/.jenkins/workspace/MyProject/target/checkout/target/my-project.tar.gz

接下来是 pom.xml 文件中有关 Maven 程序集插件的部分。我在这里将阶段设置为“安装”。此外,Jenkins 配置设置为使用“mvn package”进行构建。这很好,因为如果我进行 Jenkins 构建,则不会触发 Assembly Plugin。只有当我发布 Jenkins 时,Assembly 插件才会被触发,因为在“mvn package”之后,它会执行“mvn install”。但是,这可能不利于我提出的问题:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.3.0</version>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/assembly.xml</descriptor>
    </descriptors>
    <appendAssemblyId>false</appendAssemblyId>
    <finalName>my-project</finalName>
    <tarLongFileMode>posix</tarLongFileMode>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>install</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

这是 assembly.xml 文件:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 
          http://maven.apache.org/xsd/assembly-2.1.0.xsd">
  <!-- file formats to use when archiving: zip, tar.gz, etc.-->
  <formats>
    <format>zip</format>
    <format>tar.gz</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <!-- location of the files to archive-->
      <directory>${basedir}/src/main/webapp/resources/myfilestoarchive</directory>
      <!-- do not include folders/subfolders in the final archive -->
      <outputDirectory>./</outputDirectory>
      <!-- set permissions for all the files in the archive -->
      <fileMode>0777</fileMode>
      <!-- file types to include -->
      <includes>
        <include>*.xml</include>
        <include>*.jsp</include>
      </includes>
      <!-- specific files to leave out of archive -->
      <excludes>
        <exclude>README.txt</exclude>
        <exclude>NOTICE.txt</exclude>
      </excludes>
    </fileSet>
  </fileSets>
</assembly>

解决方案是否只是将 Maven 程序集的阶段设置在“mvn 包”之前?但是还有,我如何让插件将存档放置在项目中?

标签: gitmavenjenkinsplugins

解决方案


推荐阅读