66

我想生成一个 zip 文件,该文件将使用 maven 更新应用程序。zip 将托管在服务器上,我正在使用程序集插件生成 zip。但是我希望 maven 自动生成一个文本文件,该文件将当前版本号存储在 zip 之外。这怎么可能?

编辑:我使用 maven 程序集插件和两个描述符成功地创建了两个自定义程序集。一个有一个目录单一目标,它只是根据过滤创建一个包含更新的 version.txt 的文件夹。然后另一个有一个目标的实际上打包了 zip 文件。这似乎很不雅,我猜它不会用整个更新的文件夹正确更新 maven repo。如果有更好的方法来做到这一点,请告诉我。

4

12 回答 12

103

当然。在 src/main/resources 的某处创建一个文本文件,调用它version.txt(或其他)

文件内容:

${project.version}

现在在你的 pom.xml 中,在 build 元素中,放置这个块:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
        <include>**/version.txt</include>
      </includes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>false</filtering>
      <excludes>
        <exclude>**/version.txt</exclude>
      </excludes>
    </resource>
    ...
  </resources>
</build>

每次构建后,文件(您可以在目标/类中找到)将包含当前版本。

现在,如果您想自动将文件移动到其他位置,您可能需要通过maven-antrun-plugin执行 ant 任务。

像这样的东西:

  <build>
    ...
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
         <version>1.4</version>
         <executions>
          <execution>
            <phase>process-resources</phase>
            <configuration>
               <tasks>
                 <copy file="${project.build.outputDirectory}/version.txt"
                   toFile="..." overwrite="true" />
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
   </plugins>
 </build>
于 2010-08-20T15:18:10.797 回答
17

使用标准META-INF\MANIFEST.MF(然后您可以使用 Java 代码getClass().getPackage().getImplementationVersion()获取版本)

对于 .war 使用此配置:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <archive>                   
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
        </archive>
    </configuration>
</plugin>

这将在构建期间添加清单,或者您可以调用mvn war:manifest

另请参阅如何在运行 Tomcat 时获取软件包版本?

于 2013-03-11T03:09:44.510 回答
13

你指的是所谓的过滤

您需要对特定资源启用过滤,然后${project.version}将其用作构建的一部分

于 2010-08-20T15:16:45.277 回答
10

在 Maven 3 中,使用Sean 的答案来创建您的version.txt文件,(此处显示了我的,以及构建日期和活动配置文件):

${project.version}-${profileID}
${buildDate}

profileID为每个配置文件添加属性,例如:

<properties>
    <profileID>profileName</profileID>
</properties>

使用 Maven copy-resources 将文件复制到更容易访问的目录,例如${basedir}${basedir}/target

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}</outputDirectory>
                <resources>
                    <resource>
                        <directory>${basedir}/target/.../[version.txt dir]/version.txt</directory>
                        <includes>
                            <include>version.txt</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

输出如下所示:

1.2.3-profileName
yymmdd_hhmm
于 2015-06-30T22:35:07.713 回答
10

对于 Spring Boot 应用程序,请遵循上面接受的答案,但要替换

${project.version}

@project.version@

这是文档的链接https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering

于 2018-11-15T15:07:56.040 回答
7

您还可以使用 Groovy 脚本来生成版本信息文件。我更喜欢这种方法,因为您不必排除程序集插件描述符中的内容。您还可以使用此方法选择性地包含仅在您从 Jenkins/Hudson 构建时可用的内容(例如检查 oug BUILD_ID 等...)。

因此,您将在 pom.xml 中有一个文件生成 groovy 脚本,如下所示:

  <plugin>
    <groupId>org.codehaus.mojo.groovy</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>1.0-beta-3</version>
    <executions>
      <execution>
        <phase>test</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <source>
        <![CDATA[
        println("==== Creating version.txt ====");
        File mainDir = new File("src/main");
        if(mainDir.exists() && !mainDir.isDirectory()) {
            println("Main dir does not exist, wont create version.txt!");
            return;
        }
        File confDir = new File("src/main/conf");
        if(confDir.exists() && !confDir.isDirectory()) {
            println("Conf dir is not a directory, wont create version.txt!");
            return;
        }
        if(!confDir.exists()) {
            confDir.mkdir();
        }
        File versionFile = new File("src/main/conf/version.txt");
        if(versionFile.exists() && versionFile.isDirectory()) {
            println("Version file exists and is directory! Wont overwrite");
            return;
        }
        if(versionFile.exists() && !versionFile.isDirectory()) {
            println("Version file already exists, overwriting!");
        }
        println("Creating Version File");
        BufferedWriter writer = new BufferedWriter(new FileWriter(versionFile));

        writer.write("groupId = ${project.groupId}");
        writer.newLine();
        writer.write("artifactId = ${project.artifactId}");
        writer.newLine();
        writer.write("version = ${project.version}");
        writer.newLine();
        writer.write("timestamp = ${maven.build.timestamp}");

        String buildTag = "";
        String buildNumber = "";
        String buildId = "";
        try {
            buildTag = "${BUILD_TAG}";
            buildNumber = "${BUILD_NUMBER}";
            buildId = "${BUILD_ID}";

            writer.write("BUILD_TAG = " + buildTag + "\n");
            writer.write("BUILD_NUMBER = " + buildNumber + "\n");
            writer.write("BUILD_ID = " + buildId + "\n");

        } catch (Exception e) {
            println("============= Could not find BUILD_TAG probably this is not a Jenkins/Hudson build ===========");
        }

        writer.close();
        ]]>
          </source>
        </configuration>
      </execution>
    </executions>
  </plugin>

然后你在 pom.xml 中的程序集插件插件看起来像这样:

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <!-- Produce the all-dependencies-included jar for java classloaders -->
    <executions>
      <execution>
        <id>all</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <finalName>${project.artifactId}</finalName>
          <descriptors>
            <descriptor>dist-all.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
  </plugin>

最后你的程序集描述符 dist-all.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
  <id>all</id>
  <formats>
    <format>dir</format>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>target</directory>
      <outputDirectory></outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>src/main/conf</directory>
      <outputDirectory></outputDirectory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>
于 2011-08-12T07:08:49.110 回答
6

我只是用蚂蚁任务做到了这一点。

<echo file="version.txt">${project.version}</echo>
于 2017-07-10T22:05:22.847 回答
1

您可以使用 maven-antrun-plugin 和 regex 表达式将版本输入到文件中。PS:version.txt 文件内容为任意字符串 ex:version。

 ...
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
         <version>1.4</version>
         <executions>
          <execution>
            <phase>process-resources</phase>
            <goals>
             <goal>run</goal>
            </goals>
            <configuration>
               <target>
                 <replaceregexp file="resources/version.txt" match=".*" replace="${project.version}" byline="true" />
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>
   </plugins>
于 2020-03-13T04:27:06.423 回答
1

在 pom.xml 中添加以下插件对我有用。这只是上述答案的组合:-

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <configuration>
              <target><echo file="version.txt">${project.version}</echo> </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
于 2020-12-28T09:08:15.710 回答
0

一种可能性是将所有项目属性存储到.jar使用maven-properties-plugin.
然后,您可以使用标准(虽然不太实用)Java Properties API来读取这些属性。

        <!-- Build properties to a file -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals> <goal>write-project-properties</goal> </goals>
                    <configuration>
                        <outputFile> ${project.build.outputDirectory}/build.properties </outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

小心这种方法,因为它可能会泄漏不应该最终发布的属性,也来自settings.xml.

于 2017-04-23T11:32:39.930 回答
0

要添加到 Sean 的答案,您可以使用资源中的 targetpath 参数将版本文件移动到 jar 中的文件夹位置。以下代码在 jar 中创建了一个名为“resources”的文件夹,并在该文件夹中找到了文本文件 (version.number)。

<resource>
    <directory>resources</directory>
    <targetPath>resources</targetPath>
    <filtering>true</filtering>
    <includes>
        <include>version.number</include>
    </includes>
</resource>
<resource>
    <directory>resources</directory>
    <filtering>false</filtering>
    <excludes>
        <exclude>version.number</exclude>
    </excludes>
</resource>
于 2017-07-04T08:44:40.420 回答
0

我更喜欢write-properties-file-maven-plugin,因为我不希望所有 maven-project-properties 在一个文件中:

  <plugin>
    <groupId>com.internetitem</groupId>
    <artifactId>write-properties-file-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
      <execution>
        <id>one</id>
        <phase>compile</phase>
        <goals>
            <goal>write-properties-file</goal>
        </goals>
        <configuration>
          <filename>test.properties</filename>
          <properties>
            <property>
              <name>one</name>
              <value>1</value>
            </property>
            <property>
              <name>artifactId</name>
              <value>My Artifact ID is ${project.artifactId}</value>
            </property>
          </properties>
        </configuration>
      </execution>
    </executions>
  </plugin>
于 2018-11-12T11:47:29.410 回答