Issue
I want to use Maven Exec Plugin in order to write a command output to a file, I was only able to do it by running a shell command that the plugin is invoking.
This is the shell script:
git rev-parse --abbrev-ref HEAD > branch.txt
This is my pom.xml:
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<inherited>false</inherited>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>5.3.21</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>gitbranch.sh</executable>
<workingDirectory>${basedir}</workingDirectory>
</configuration>
</plugin>
When trying to run it as a command and not using a script , I can't get to be written into a file:
<configuration>
<executable>git</executable>
<outputFile>branching.txt</outputFile>
<arguments>
<argument>rev-parse</argument>
<argument>--abbrev-ref</argument>
<argument>HEAD</argument>
</arguments>
<workingDirectory>${basedir}</workingDirectory>
</configuration>
I also tried to run echo "something" > file.txt
using this plugin, but nothing is writing anything in to a file.
Solution
This may not directly answer your question but I use maven git commit id plugin to do the same thing you want to do. I have:
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<useNativeGit>true</useNativeGit>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssXXX</dateFormat>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
</configuration>
</plugin>
in my build/plugins
section of my pom.xml
. When I run this I get the file named git.properties
generated in my target/classes
directory that contains:
#Generated by Git-Commit-Id-Plugin
#Wed Jul 06 11:51:45 MDT 2022
git.branch=feature/test-branch
git.build.host=bluerock
git.build.time=2022-07-06T11\:51\:45-06\:00
git.build.user.email=
git.build.user.name=
git.build.version=1.0
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=c991301b1a88e6cd138d347c46e3479d34b6f24d
git.commit.id.abbrev=c991301
git.commit.id.describe=c991301
git.commit.id.describe-short=c991301
git.commit.message.full=cleaned up a bit
git.commit.message.short=cleaned up a bit
git.commit.time=2020-09-29T11\:48\:58-06\:00
[email protected]
git.commit.user.name=First Last
git.dirty=false
git.remote.origin.url=<the git url>
git.tags=
git.total.commit.count=4
Then, in my code base I have:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class VersionInfo {
public String getVersionString() {
try {
Properties properties = getGitProperties();
boolean isDirty = false;
String gitDirty = properties.getProperty( "git.dirty" );
if( gitDirty != null )
isDirty = Boolean.parseBoolean(gitDirty);
return "built \"" + properties.getProperty("git.build.time") +
"\" in branch \"" + properties.getProperty("git.branch") +
"\" with short commit id \"" + properties.getProperty("git.commit.id.describe-short") + "\"" +
", isDirty is " + isDirty +
" remote url is \"" + properties.getProperty("git.remote.origin.url") + "\"";
}
catch( IOException ioe ) {
return( "can't locate git.properties on the class path");
}
}
private Properties getGitProperties() throws IOException {
Properties properties = new Properties();
try (InputStream inputStream = this.getClass().getResourceAsStream("/git.properties")) {
if (inputStream == null)
throw new IOException("Can't locate properties file to generate version info");
properties.load(inputStream);
return properties;
}
}
}
This allows me to print out information about the git environment as part of a startup string or do what you want with it. Again
Answered By - stdunbar
Answer Checked By - David Marino (JavaFixing Volunteer)