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

时间:2010-08-20 15:03:55

标签: java maven-2 maven-assembly-plugin

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

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

12 个答案:

答案 0 :(得分:85)

不确定。在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>

答案 1 :(得分: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?

答案 2 :(得分:11)

您所指的是filtering

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

答案 3 :(得分: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>

答案 4 :(得分: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

答案 5 :(得分:2)

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

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

答案 6 :(得分:2)

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

${project.version}

使用

@project.version@

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

的链接

答案 7 :(得分: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>

答案 8 :(得分: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

答案 9 :(得分:0)

要添加到Sean的答案,您可以使用resource中的targetpath参数将版本文件移动到jar中的文件夹位置。以下代码创建了一个名为&#39; resources&#39;的文件夹。在jar文件夹中找到文本文件(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>

答案 10 :(得分: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>

答案 11 :(得分:0)

在 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>
  [...]