1

Test.java is the class that has the main method

I have added Test.java in pom.xml. So that after the maven build, the main method will be called.

Below code snippet, denotes the class name in pom.xml

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

I am running the project using mvn clean install exec:java command.

As expected, Test.java is called after a successful maven build.

Is there any option to run Test.java even after maven build failure?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
niranj1997
  • 719
  • 8
  • 16
  • 2
    What about just running, mvn exec:java ? That should run your main without compiling if that is what you're asking? Although, I think using clean will remove your class files, so after a failed build there might not be enough to run. – matt Mar 30 '21 at 12:51
  • 1
    at what phase did your build failed? Did you try: `mvn compile exec:java -Dexec.mainClass="com.example.Test`. In this case, you `compile` and force an execution on a specific class. If your question is how to execute the class even if the build *phase X* fails (obviously after compilation), then check `true`. – azbarcea Mar 30 '21 at 12:52
  • Thanks, matt & azbarcea! After trying out both suggesstions, azbarcea soulution helped me out! Thanks for your help! :) – niranj1997 Mar 30 '21 at 13:01
  • Does this answer your question? [How to declare a before and after execution of a maven plugin around another plugin execution?](https://stackoverflow.com/questions/1467509/how-to-declare-a-before-and-after-execution-of-a-maven-plugin-around-another-plu) – talex Mar 30 '21 at 13:22
  • No talex!. But I got the solution(comment) from azbarcea! – niranj1997 Mar 30 '21 at 14:34

2 Answers2

0

Consider using Java Spring-Boot Command line runner:

@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]);
        }
    }
}

The code in the public void run() will be called once the server started.

roeygol
  • 4,908
  • 9
  • 51
  • 88
  • Thanks for your answer. But we are not using spring boot here. My only need is to run my main method after maven success (or) failure – niranj1997 Mar 30 '21 at 12:40
0

As per @azbarcea's comment, tried adding the following snippet to pom.xml properties

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

After adding the above code, Test.java is called and it is getting executed.

niranj1997
  • 719
  • 8
  • 16