带有ClassNotFoundException的Exec Maven插件

时间:2018-01-31 09:31:47

标签: java maven exec-maven-plugin

我有一个多模块(模型和服务模块)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>

在&#34;模型&#34;模块我有一个类名ABCEntity.java,在服务模块中,我想扫描ABCEntity.java并生成一些样板类。

&#34;服务&#34;模块对&#34;模型&#34;具有maven依赖性。模块加上对代码生成器模块的依赖(外部应用程序)。

当我运行&#34; mvn exec:java&#34;在&#34;服务&#34;模块,我收到了一些错误,找不到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$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

有人可以帮我吗?我不明白为什么无法找到ABCEntity,因为:  1)ABCEntity在同一个项目中但在另一个模块中  2)我已经声明了对该模块的依赖。

2 个答案:

答案 0 :(得分:0)

似乎一旦我使用了这个插件的另一个目标(我只是执行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>

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

答案 1 :(得分:-1)

您可以访问this

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