如何在maven构建之前运行一个类?

时间:2016-07-15 16:49:23

标签: java maven

以下是什么导致构建失败? 我创建了一个应该在 maven开始执行构建之前运行的类。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.5.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>my.package.CustomClass</mainClass>
            </configuration>
        </plugin>

导致构建失败:

java.lang.ClassNotFoundException: my.package.CustomClass
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

只需更改maven执行的阶段(如'test'或'install'),所以当插件运行类时,它将在classpath中可用。(如果你真的无法将其移动到另一个依赖项)

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.5.0</version>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>my.package.CustomClass</mainClass>
            </configuration>
        </plugin>