使用maven将版本号输出到文本文件 [英] Using maven to output the version number to a text file

查看:1595
本文介绍了使用maven将版本号输出到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to generate an 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. Is this possible?

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

I successfully did it using the maven Assembly Plugin and two descriptor 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 up date the maven repo with the whole updated folder. If there is a better way to do this please let me know.

推荐答案

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

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

文件内容:

${project.version}

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

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

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

现在,如果你想自动将文件移到其他地方,你可能需要通过 maven-antrun-plugin

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.

Something像这样:

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>

这篇关于使用maven将版本号输出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆