Search code examples
javamavenexec-maven-plugin

Exec Maven Plugin with ClassNotFoundException


I have a multi-module(model and service module) maven project:

model
|_____ABCEntity.java
service
|_____pom.xml
      <dependency>model</dependency>
      <dependency>code-generation</dependency>
      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <configuration>
            <mainClass>com.codegenerator.CodeGeneratorApplication</mainClass>
          </configuration>
      </plugin>

In the "model" module I have a class name ABCEntity.java and in the service module, I want to scan ABCEntity.java and generate some boilerplate classes.

The "service" module has a maven dependency to the "model" module plus a dependency to the code generator module(external application).

When I run "mvn exec:java" in the "service" module, I got some error that the ABCEntity.java is not found:

    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Failed to execute ApplicationRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:770)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:757)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at codegenerator.CodeGeneratorApplication.main(CodeGeneratorApplication.java:26)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: ABCEntity
 

Could anyone help me with this? I do not understand why the ABCEntity cannot be found, because: 1)ABCEntity is in the same project but in another module 2)I have already declared the dependency to that module.


Solution

  • It seems once I use the other goal(I just execute mvn exec:exec) of this plugin, the ABCEntity can be found:

                <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <!-- automatically creates the classpath using all project dependencies, 
                            also adding the project build directory -->
                        <classpath />
                        <argument>codegenerator.CodeGeneratorApplication</argument>
                        ...
                    </arguments>
                </configuration>
    
            </plugin>
    

    But I am not sure about the real reason behind.