Java 如何在 Maven 中显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3416573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:08:05  来源:igfitidea点击:

How can I display a message in Maven

javamaven-2

提问by David

How can I display a message in Maven?

如何在 Maven 中显示消息?

In ant, we do have "echo" to display a message, but in maven, how can I do that?

在 ant 中,我们确实有“echo”来显示消息,但是在 maven 中,我该怎么做?

回答by matt b

You can use the antrun plugin:

您可以使用 antrun 插件:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>Hello world!</echo>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

One issue though is that you have to choose what phase of the build lifecycleto bind this to (my example has the plugin bound to generate-resources). Unlike Ant, you aren't controlling the lifecycle yourself, but rather just binding plugins to certain points in a pre-defined lifecycle. Depending on what you are actually trying to do, this may or may not make sense for your use case.

但是,一个问题是您必须选择构建生命周期的哪个阶段将绑定到(我的示例将插件绑定到generate-resources)。与 Ant 不同,您不是自己控制生命周期,而只是将插件绑定到预定义生命周期中的某些点。根据您实际尝试执行的操作,这可能对您的用例有意义,也可能没有意义。

回答by Venkatesh Dayawar

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>[your message]:${Urkey}</echo>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

回答by UnclickableCharacter

You can use Groovy Maven Pluginfor this.

您可以为此使用Groovy Maven 插件

<plugin>                                                         
    <groupId>org.codehaus.gmaven</groupId>                       
    <artifactId>groovy-maven-plugin</artifactId>                 
    <version>2.0</version>                                       
    <executions>                                                 
        <execution>                                              
            <phase>validate</phase>                              
            <goals>                                              
                <goal>execute</goal>                             
            </goals>                                             
            <configuration>                                      
                <source>                                         
                    log.info('Test message: {}', 'Hello, World!')
                </source>                                        
            </configuration>                                     
        </execution>                                             
    </executions>                                                
</plugin>                                                        

The configuration above will produce the following output:

上面的配置将产生以下输出:

[INFO] Test message: Hello, World!

回答by Ben Hutchison

You can use Bj?rn Ekryd's Echo Maven Plugin, which is published in Maven Central:

您可以使用Bj?rn EkrydEcho Maven 插件,该插件Maven Central 中发布:

<plugin>
    <groupId>com.github.ekryd.echo-maven-plugin</groupId>
    <artifactId>echo-maven-plugin</artifactId>
    <version>1.2.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>echo</goal>
            </goals>
            <configuration>
                <message>war has changed</message>
            </configuration>
        </execution>
    </executions>
</plugin>


[INFO] --- maven-war-plugin:2.4:war (default-war) @ mymodule ---
[INFO] Packaging webapp
[INFO] Processing war project
[INFO]
[INFO] --- echo-maven-plugin:1.2.0:echo (default) @ mymodule ---
[INFO] war has changed
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Also, this plugin has 95% code coverage, which is pretty cool.

此外,这个插件有95% 的代码覆盖率,这很酷。