我想生成一个 zip 文件,该文件将使用 Maven 更新应用程序。该 zip 将托管在服务器上,我使用程序集插件来生成 zip。不过,我希望 maven 自动生成一个文本文件,将当前版本号存储在 zip 之外。这怎么可能?
编辑:
我使用 Maven 程序集插件和两个描述符成功地创建了两个自定义程序集。其中一个目标是单一目录,它只是根据过滤创建一个包含更新的 version.txt 的文件夹。然后,另一个具有单一目标的文件实际上会打包 zip 文件。这似乎非常不优雅,我想它不会使用整个更新的文件夹正确更新 Maven 存储库。如果有更好的方法来做到这一点,请告诉我。
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?
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.
发布评论
评论(14)
当然。在 src/main/resources 中的某个位置创建一个文本文件,将其命名为
version.txt
(或其他名称)文件内容:
现在在 pom.xml 中的 build 元素内,放置此块:
在每次构建之后,该文件(您可以在目标/类中找到)将包含当前版本。
现在,如果您想自动将文件移动到其他位置,您可能需要通过 maven-antrun-plugin.
像这样的事情:
Sure. Create a text file somewhere in src/main/resources, call it
version.txt
(or whatever)File content:
now in your pom.xml, inside the build element, put this block:
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.
Something like this:
使用标准
META-INF\MANIFEST.MF
(然后您可以使用 Java 代码getClass().getPackage().getImplementationVersion()
获取版本)对于 .war 使用此配置:
这将在构建期间添加清单,或者您可以调用
mvn war:manifest
另请参阅如何在运行Tomcat时获取包版本?
Use standard
META-INF\MANIFEST.MF
(Then you can use Java codegetClass().getPackage().getImplementationVersion()
to get version)For .war use this configuration:
That will add manifest during build, or you can call
mvn war:manifest
See also How to get package version at running Tomcat?
您所指的称为 过滤
您需要启用对特定资源的过滤,然后使用
${project.version}
,它将被替换为构建的一部分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在 Maven 3 中,使用 Sean 的回答 创建您的
version.txt
文件,(我的显示为此处,以及构建日期和活动配置文件):将属性
profileID
添加到每个配置文件,例如:使用 Maven 复制资源将文件复制到更容易访问的目录,例如
$ {basedir}
或${basedir}/target
:输出如下所示:
in Maven 3, Use Sean's answer to create your
version.txt
file, (mine is shown here, along with build date and active profile):adding property
profileID
to each of the profiles, e.g.:Use Maven copy-resources to copy the file to an easier to reach directory such as
${basedir}
or${basedir}/target
:output looks like this:
对于 Spring Boot 应用程序,请遵循上面接受的答案,但替换
为
这里是文档的链接 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering< /a>
For a Spring Boot application, follow the accepted answer from above however substituting
with
Here's the link to the documentation https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering
您还可以使用 Groovy 脚本来生成版本信息文件。我更喜欢这种方法,因为您不必排除程序集插件描述符中的内容。您还可以使用此方法选择性地包含仅在从 Jenkins/Hudson 构建时可用的内容(例如检查 oug BUILD_ID 等...)。
因此,您将在 pom.xml 中有一个文件生成 groovy 脚本,如下所示:
然后 pom.xml 中的程序集插件将如下所示:
最后,您的程序集描述符 dist-all.xml 将如下所示:
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...).
So you would have a file-generating groovy script in pom.xml like this:
And then your assembly plugin plugin in pom.xml that would look like this:
And finally your assembly descriptor dist-all.xml would look like this:
我刚刚用蚂蚁任务完成了这个。
I just did this with an ant task.
只需使用 maven-help-plugin 即可。该版本将位于
target/version.txt
(或您正在使用的任何构建目录)源
Just use the maven-help-plugin. The version will be in
target/version.txt
(or whatever build directory you are using)Source
我更喜欢 write-properties-file-maven-plugin,因为我不希望所有 maven-project-properties 都在一个文件中:
I prefer the write-properties-file-maven-plugin, because I don't want all maven-project-properties in one file:
您可以使用 maven-antrun-plugin 和正则表达式将版本输入到文件中。
PS:version.txt文件内容是任意字符串ex:version。
you can use the maven-antrun-plugin and regex expressions to input the version into the file.
PS: version.txt file content is any string ex:version.
一种可能性是使用
maven-properties-plugin
将所有项目属性存储到构建的.jar
中。然后您可以使用标准(尽管不太实用)读取这些属性 Java 属性 API。
请小心使用此方法,因为它可能会泄漏不应该最终发布的属性(也来自
settings.xml
)。One possibility is to store all project properties to the built
.jar
usingmaven-properties-plugin
.Then you can read these properties using standard (though not too practical) Java Properties API.
Be careful with this approach as it may leak properties that are not supposed to end up published, also from
settings.xml
.在 pom.xml 中添加以下插件对我有用。这只是上述答案的组合:-
Adding below plugin in pom.xml worked for me. This is a combination of above answers only:-
要添加到 Sean 的答案中,您可以使用资源中的 targetpath 参数将版本文件移动到 jar 中的文件夹位置。以下代码在 jar 中创建一个名为“resources”的文件夹,并在该文件夹中找到文本文件 (version.number)。
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.
在 /src/version.txt 中创建包含以下内容的 version.txt
使用程序集插件并在 dist.xml 中添加带有filtering=true的步骤
Create version.txt with following content in /src/version.txt
Use assembly plugin and add step in dist.xml with filtering=true