使用 ClassNotFoundException 执行 Maven 插件

Exec Maven Plugin with ClassNotFoundException

我有一个多模块(模型和服务模块)maven 项目:

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>

在“模型”模块中,我有一个 class 名称 ABCEntity.java,在服务模块中,我想扫描 ABCEntity.java 并生成一些样板文件 classes .

“服务”模块具有对“模型”模块的 Maven 依赖性以及对代码生成器模块(外部应用程序)的依赖性。

当我在“服务”模块中 运行 "mvn exec:java" 时,出现一些错误,指出 ABCEntity.java 未找到:

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

谁能帮我解决这个问题?我不明白为什么找不到 ABCEntity,因为: 1)ABCEntity在同一个项目中,但在另一个模块中 2)我已经声明了对该模块的依赖。

您可以访问this

<executableDependency>
    <groupId>your groupId</groupId>
    <artifactId>>model</artifactId>
</executableDependency>
<mainClass>com.codegenerator.CodeGeneratorApplication</mainClass>

好像我用这个插件的另一个目标(我只是执行mvn exec:exec),ABCEntity可以找到:

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

但我不确定背后的真正原因。