我想执行 git describe
作为 Maven 构建的一部分,并在 list 中使用生成的输出来构建 .jar 包。
我知道how to do this in ant通过 <exec>
task与 outputproperty
到 ant 属性变量,但我对 Maven 的经验很少,甚至不知道去哪里找。
这可能吗?
我在示例 pom.xml 文件中找到了它,所以 adding something to the manifest看起来很简单:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>my.class.here.Myclass</mainClass>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
虽然不确定如何捕获命令执行。
最佳答案
这是一个建议的方法:
- 使用Exec Maven Plugin用于启动您的 git 命令并写入属性文件(如果可能,名称=值模式)
- 使用Properties Maven Plugin加载配置
加载的配置可以用作 POM 中的属性。我们基本上是在动态创建我们构建的属性。为此(使用这些属性),必须在构建流程中尽早执行上述步骤(即 validate
或 initialize
阶段)。
下面是一个流程示例,刚刚测试并完美运行(在 Windows 机器上):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>generation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>retrieve-config</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>echo</executable>
<arguments>
<argument>jar.name=from-exec</argument>
<argument>></argument>
<argument>config.properties</argument>
</arguments>
<workingDirectory>${basedir}/src/main/resources/</workingDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>read-properties</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/config.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${jar.name}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
基本上,附加到 validate
的 exec 插件阶段将在构建开始时执行,将内容写入 config.properties 文件(通过 echo 命令)jar.name=from-exec
.
然后附加到initialize
的属性插件阶段将读取该 config.properties 文件并加载要用作构建一部分的属性。
然后,作为示例,jar 插件将使用该属性作为其配置的一部分(<finalName>${jar.name}</finalName>
部分)。
正在运行 mvn clean package
, 你会发现 from-exec.jar
目标文件夹中的文件。
如果您无法获得 git describe
的结果作为名称=值模式,您可以(最坏的情况)执行两次 Exec Maven 插件,第一次将属性名称和等号字符写入文件(即通过回显),第二次( git describe
)附加到文件属性值。
关于java - 让 Maven 执行程序并将输出存储到可在 .jar list 中使用的属性中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34232828/