如何在Maven中显示消息

时间:2010-08-05 15:52:01

标签: java maven-2

如何在Maven中显示消息?

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

4 个答案:

答案 0 :(得分:48)

您可以使用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>

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

答案 1 :(得分:12)

您可以使用Groovy Maven Plugin

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

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

[INFO] Test message: Hello, World!

答案 2 :(得分:10)

您可以使用Björn Ekryd中发布的Echo Maven Plugin 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] ------------------------------------------------------------------------

此外,这个插件有95% code coverage,非常酷。

答案 3 :(得分:4)

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