Maven exec插件 - 当classpathscope = test时如何从main / java导入类

时间:2012-04-04 11:20:02

标签: java testing maven maven-2 exec

我有一个项目,其中Main-Method-class“Bootstrap.class”位于src / test文件夹中。所以我使用<classpathScope>test</classpathScope> 能够执行

mvn exec:java

我的问题:我得到一个“java.lang.IllegalArgumentException:未知实体:”因为我导入了某些类,它们位于main / java。关于如何解决这个问题的任何想法?

这是我的Pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>eu.peadrtx</groupId>
<artifactId>register</artifactId>
<packaging>war</packaging>
<version>1.6-SNAPSHOT</version>
<name>eu.peadrtx.register</name>

<properties>
    <!-- Convenience property to set the GWT version -->
    <gwtVersion>2.4.0</gwtVersion>

    <!-- GWT needs at least java 1.6 -->
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>

    <!-- Don't let your Mac use a crazy non-standard encoding -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<!-- notwendig für einige gwt dependencies -->
<repositories>
    <repository>
        <id>gwt-maven-rewraps</id>
        <url>http://gwt-maven-rewraps.googlecode.com/hg/</url>
    </repository>
    <repository>
        <id>cobogw</id>
        <name>Maven2 Repository for cobogw library</name>
        <url>http://cobogw.googlecode.com/svn/maven2</url>
    </repository>
    <repository>
        <id>openehealth.releases</id>
        <name>Open eHealth Maven Repository</name>
        <url>http://repo.openehealth.org/maven2/releases</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>openehealth.snapshots</id>
        <name>Open eHealth Maven Repository</name>
        <url>http://repo.openehealth.org/maven2/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>java.net</id>
        <name>java.net Maven Repository</name>
        <url>http://download.java.net/maven/2/</url>
    </repository>
    <repository>
        <id>codehaus nexus</id>
        <name>Codehaus Nexus Maven Repository</name>
        <url>https://nexus.codehaus.org/content/repositories/releases/</url>
    </repository>
    <repository>
        <id>codehaus</id>
        <name>Codehaus Maven Repository</name>
        <url>http://repository.codehaus.org</url>
    </repository>
    <repository>
        <id>jboss</id>
        <name>JBoss Maven Repository</name>
        <url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
    <repository>
        <id>hapi-sf</id>
        <name>HAPI Sourceforge Repository</name>
        <url>http://hl7api.sourceforge.net/m2</url>
    </repository>
</repositories> 

<dependencies>

    ...

    <!-- dependencies for the Unit tests -->

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-dev</artifactId>
        <version>${gwtVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20080701</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.5</version>
    </dependency>       
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.15.0</version>
    </dependency>


</dependencies>

<build>
    <!-- Generate compiled stuff in the folder used for development mode -->
    <outputDirectory>target/www/WEB-INF/classes</outputDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    <plugins>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>2.0.5</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
        </plugin>

        <!-- GWT Maven Plugin -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>gwt-maven-plugin</artifactId>
            <version>2.4.0</version>
            <dependencies>
                <dependency>
                    <groupId>com.google.gwt</groupId>
                    <artifactId>gwt-user</artifactId>
                    <version>${gwtVersion}</version>
                </dependency>
                <dependency>
                    <groupId>com.google.gwt</groupId>
                    <artifactId>gwt-dev</artifactId>
                    <version>${gwtVersion}</version>
                </dependency>
                <dependency>
                    <groupId>com.google.gwt</groupId>
                    <artifactId>gwt-servlet</artifactId>
                    <version>${gwtVersion}</version>
                </dependency>
            </dependencies>
            <!-- JS is only needed in the package phase, this speeds up testing -->
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>compile</goal>
                        <goal>test</goal> 
                     </goals>
                </execution>  
            </executions>

            <!-- Plugin configuration. There are many available options, see gwt-maven-plugin 
                documentation at codehaus.org -->

            <!-- add gwt:18n and gwt:css configuration see -> http://mojo.codehaus.org/gwt-maven-plugin/i18n-mojo.html#i18nConstantsBundles -->
            <configuration>
                <extraJvmArgs>-javaagent:target/www/WEB-INF/lib/lombok-0.10.8.jar=ECJ -XX:MaxPermSize=384m -Xmx512m -Xss124m</extraJvmArgs>
                <!-- URL that should be automatically opened in the GWT shell (gwt:run). -->
                <runTarget>Register.html</runTarget>
                <!-- Location of the develop-mode web application structure (gwt:run). -->
                <hostedWebapp>target/www</hostedWebapp>
                <!-- Ask GWT to create the Story of Your Compile (SOYC) (gwt:compile) -->
                <compileReport>true</compileReport>
                <module>eu.paedrtx.register.Register</module>
                <logLevel>INFO</logLevel>
                <style>${gwt.style}</style>
            </configuration>
        </plugin>

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>
          <executions>
            <execution>
              <id>my-execution</id>
              <phase>prepare-package</phase>
              <goals>
                <goal>java</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <mainClass>eu.paedrtx.register.server.utility.Bootstrap</mainClass>
            <classpathScope>test</classpathScope>
          </configuration>
        </plugin>

        <!-- Add source folders to test classpath in order to run gwt-tests as 
            normal junit-tests -->
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${project.build.sourceDirectory}</additionalClasspathElement>
                    <additionalClasspathElement>${project.build.testSourceDirectory}</additionalClasspathElement>
                </additionalClasspathElements>
                <useManifestOnlyJar>false</useManifestOnlyJar>
                <forkMode>always</forkMode>
                <!-- Folder for generated testing stuff -->
                <systemProperties>
                    <property>
                        <name>gwt.args</name>
                        <value>-out ${project.build.directory}/${project.build.finalName}</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
        <!-- Copy static web files before executing gwt:run-->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.2</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/www</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/webapp</directory>
                            </resource>
                        </resources>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </execution>
            </executions>
        </plugin>
         <!-- Delete gwt generated stuff -->
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.3</version>
            <configuration>
            <filesets>
                <fileset><directory>src/main/webapp/register</directory></fileset>
                <fileset><directory>src/main/webapp/WEB-INF/classes</directory></fileset>
                <fileset><directory>tomcat</directory></fileset>
                <fileset><directory>www-test</directory></fileset>
                <fileset><directory>.gwt-tmp</directory></fileset>
            </filesets>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.7</version>  <!--  Note 2.8 does not work with AspectJ aspect path -->
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
                <wtpversion>2.0</wtpversion>
                <additionalBuildcommands>
                <buildCommand>
                   <name>com.google.gwt.eclipse.core.gwtProjectValidator</name>
                </buildCommand>
                </additionalBuildcommands>
                <additionalProjectnatures>
                    <projectnature>com.google.gwt.eclipse.core.gwtNature</projectnature>
                </additionalProjectnatures>
            </configuration>
         </plugin>
    </plugins>
</build>

stacktrace:

[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ register ---
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.IllegalArgumentException: Unknown entity: eu.paedrtx.register.server.domain.PaymentDetails
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:675)
    at eu.paedrtx.register.server.repository.Repository.create(Repository.java:54)
    at eu.paedrtx.register.server.utility.Bootstrap.main(Bootstrap.java:75)
    ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.299s
[INFO] Finished at: Wed Apr 04 13:04:14 CEST 2012
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project register: An exception occured while executing the Java class. null: InvocationTargetException: Unknown entity: eu.paedrtx.register.server.domain.PaymentDetails -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project register: An exception occured while executing the Java class. null
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:352)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.IllegalArgumentException: Unknown entity: eu.paedrtx.register.server.domain.PaymentDetails
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:675)
    at eu.paedrtx.register.server.repository.Repository.create(Repository.java:54)
    at eu.paedrtx.register.server.utility.Bootstrap.main(Bootstrap.java:75)
    ... 6 more

2 个答案:

答案 0 :(得分:0)

尝试使用exec目标而非java使用classpath参数,如此处所述http://mojo.codehaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html

这将分叉JVM并且可以解决许多问题,特别是对于像Hibernate这样的工具来做非平凡的类加载。这是因为Maven不必使用classloader magic在进程中提供正确的类路径(Maven是java进程本身,它以简单的类路径启动,仅包括maven bootstrap jar)。这样它就会填充-cp参数,并分叉新进程:简单易行的诊断。

答案 1 :(得分:0)

我解决了这个问题。 问题是我有2个persistence.xml用于测试/资源测试,另一个用于main / resources中的应用程序。我不得不删除测试/资源中的那个,并在应用程序persistence.xml中添加其他持久性单元。

请参阅:How to configure JPA for testing in Maven