使用maven将版本号输出到文本文件

问题描述 投票:48回答:10

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

编辑:我成功地使用maven Assembly Plugin和两个描述符来创建两个自定义程序集。一个目录单一目标,它只是根据过滤创建一个带有更新版本.txt的文件夹。然后另一个具有单个目标的实际打包zip文件。这似乎非常不优雅,我想它将无法正确更新整个更新文件夹的maven repo。如果有更好的方法,请告诉我。

java maven-2 maven-assembly-plugin
10个回答
87
投票

当然。在src / main / resources中的某处创建一个文本文件,称之为version.txt(或其他)

文件内容:

${project.version}

现在在您的pom.xml中,在构建元素内部,放置此块:

<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>

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>

16
投票

使用标准的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

另见How to get package version at running Tomcat?


12
投票

你所指的是filtering

您需要对特定资源启用过滤,然后使用${project.version},它将作为构建的一部分进行替换


6
投票

您还可以使用Groovy脚本生成版本信息文件。我更喜欢这种方法,因为你不必在assembly-plugin的描述符中排除东西。您也可以使用此方法选择性地包含仅在从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>

6
投票

在Maven 3中,使用Sean's answer创建你的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

2
投票

我只是用蚂蚁任务做了这件事。

<echo file="version.txt">${project.version}</echo>

2
投票

对于Spring Boot应用程序,请按照上面接受的答案替换

${project.version}

@project.version@

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


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


0
投票

要添加到Sean的答案,您可以使用resource中的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>
© www.soinside.com 2019 - 2024. All rights reserved.