maven exec插件:可以将命令输出写入文件

发布于 2025-02-13 08:36:17 字数 1874 浏览 0 评论 0 原文

我想使用maven exec插件来将命令输出写入文件, 我只能通过运行插件正在调用的shell命令来做到这一点。

这是shell脚本:

git rev-parse --abbrev-ref HEAD > branch.txt

这是我的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>

试图以命令而不使用脚本时,我无法写入文件:

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

我还尝试运行 echo“ sosings”&gt; file.txt 使用此插件,但没有任何内容在文件中写入任何内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-02-20 08:36:17

这可能不会直接回答您的问题,但我使用做您想做的同样的事情。我有:

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

在我的 build/plugins 我的 pom.xml 的部分。运行此操作时,我将获取名为 git.properties 在我的 target/class/class 目录中生成的

#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

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

文件 将有关GIT环境的信息作为启动字符串的一部分,或者对其做您想做的事情。再次

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文