在 Maven 构建过程之后调用 Java main 方法

Call Java main method after maven build process

Test.java 是有 main 方法的class

我在pom.xml中添加了Test.java。这样maven构建之后,就会调用main方法

下面的代码片段,表示 pom.xml

中的 class 名称
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.6.0</version>
  <configuration>
    <mainClass>com.example.Test</mainClass>
  </configuration>
</plugin>

我正在使用 mvn clean install exec:java 命令 运行 宁项目。

正如预期的那样,Test.java 在成功构建 maven 后被调用。

有没有 运行 Test.java 甚至在 maven 构建失败后的选项?

考虑使用 Java Spring-引导命令行运行程序:

@SpringBootApplication
public class SpringBootConsoleApplication 
  implements CommandLineRunner {

    private static Logger LOG = LoggerFactory
      .getLogger(SpringBootConsoleApplication.class);

    public static void main(String[] args) {
        LOG.info("STARTING THE APPLICATION");
        SpringApplication.run(SpringBootConsoleApplication.class, args);
        LOG.info("APPLICATION FINISHED");
    }
 
    @Override
    public void run(String... args) {
        LOG.info("EXECUTING : command line runner");
 
        for (int i = 0; i < args.length; ++i) {
            LOG.info("args[{}]: {}", i, args[i]);
        }
    }
}

服务器启动后将调用 public void run() 中的代码。

根据@azbarcea 的评论,尝试将以下代码段添加到 pom.xml 属性

<maven.test.failure.ignore>true</maven.test.failure.ignore>

添加上述代码后,Test.java被调用并执行。