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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3532135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:35:39  来源:igfitidea点击:

Using maven to output the version number to a text file

javamaven-2maven-assembly-plugin

提问by sanz

I want to generate a zip file that will update an application with maven. The zip will be hosted on a server and I am using the assembly plugin to generate the zip. However I would like maven to automatically generate a text file that stores the current version number outside the zip. How is this possible?

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

EDIT: I successfully did it using the maven Assembly Plugin and two descriptors to create two custom assemblies. One has a directory-single goal and it just creates a folder with the updated version.txt based on filtering. Then another one with a single goal actually packages the zip file. This seems to be very inelegant and I guess it will not properly update the maven repo with the whole updated folder. If there is a better way to do this, please let me know.

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

采纳答案by Sean Patrick Floyd

Sure. Create a text file somewhere in src/main/resources, call it version.txt(or whatever)

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

File content:

文件内容:

${project.version}

now in your pom.xml, inside the build element, put this block:

现在在你的 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>

after every build, the file (which you can find in target/classes) will contain the current version.

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

Now if you want to move the file somewhere else automatically, you are probably going to need to execute an ant task via the maven-antrun-plugin.

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

Something like this:

像这样的东西:

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

回答by Jon Freedman

What you are referring to is called filtering

您所指的称为过滤

You need to enable filtering on a particular resource, and then use ${project.version}which will be substituted as part of your build

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

回答by DeusAquilus

You could also use a Groovy script to produce a version info file. I like this method more because you don't have to exclude stuff in the assembly-plugin's descriptor. You can also use this method to optionally include stuff only available if you are building from Jenkins/Hudson (e.g. check oug BUILD_ID etc...).

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

So you would have a file-generating groovy script in pom.xml like this:

因此,您将在 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>

And then your assembly plugin plugin in pom.xml that would look like this:

然后你在 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>

And finally your assembly descriptor dist-all.xml would look like this:

最后,您的程序集描述符 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>

回答by Paul Verest

Use standard META-INF\MANIFEST.MF(Then you can use Java code getClass().getPackage().getImplementationVersion()to get version)

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

For .war use this configuration:

对于 .war 使用此配置:

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

That will add manifest during build, or you can call mvn war:manifest

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

See also How to get package version at running Tomcat?

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

回答by ekangas

in Maven 3, Use Sean's answerto create your version.txtfile, (mine is shown here, along with build date and active profile):

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

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

adding property profileIDto each of the profiles, e.g.:

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

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

Use Maven copy-resources to copy the file to an easier to reach directory such as ${basedir}or ${basedir}/target:

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

output looks like this:

输出如下所示:

1.2.3-profileName
yymmdd_hhmm

回答by Ondra ?i?ka

One possibility is to store all project properties to the built .jarusing maven-properties-plugin.
Then you can read these properties using standard (though not too practical) Java Properties API.

一种可能性是将所有项目属性存储到.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>

Be careful with this approach as it may leak properties that are not supposed to end up published, also from settings.xml.

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

回答by learmo

To add to Sean's answer, you can move the version file to a folder location within the jar by using the targetpath parameter within resource. The following code creates a folder called 'resources' within the jar and the text file (version.number) is found in that folder.

要添加 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>

回答by Dr.Octoboz

I just did this with an ant task.

我只是用蚂蚁任务完成了这个。

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

回答by Gambotic

I prefer the write-properties-file-maven-plugin, because I don't want all maven-project-properties in one file:

我更喜欢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>

回答by Vuong Ly

For a Spring Boot application, follow the accepted answer from above however substituting

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

${project.version}

with

@project.version@

Here's the link to the documentation https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering

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