我正在尝试运行一个Java类,该类将虚拟数据填充到我的数据库中。在Eclipse中,我只需右键单击并作为Java程序运行即可。问题是我想让詹金斯去做...明显的解决方案是使用maven运行一个类,因为它将所需的所有内容都放在了类路径上。

我已经这样尝试过http://mojo.codehaus.org/exec-maven-plugin/

<profile>
    <id>populatedb</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <property>
                <name>populatedb</name>
            </property>
        </activation>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.example.DatasetReader</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>


但是它甚至在项目建立之前就给了我ClassNotFound on com.example.DatasetReader。我使用以下命令:

mvn clean install exec:java -Dpopulatedb -Dclasspath -Dexec.mainClass="com.example.DatasetReader"


我认为它必须与执行阶段有关...但是没有像安装后这样的东西...

谢谢!

最佳答案

我认为问题与exec-maven-plugin使用的类路径有关。默认情况下,exec-maven-plugin使用runtime类路径。我假设您的DatasetReader类是测试类,因此仅在test类路径上可用。

要将不同的类路径传递给exec-maven-plugin,请使用classpathScope property

因此,您可以在pom中使用<classpathScope>test</classpathScope>使插件与测试类路径一起运行。

因此,您只需要将您的POM修改如下:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <classpathScope>test</classpathScope> <!-- this is the extra config -->
        <mainClass>com.example.DatasetReader</mainClass>
    </configuration>
</plugin>

07-24 09:45
查看更多