2012-04-04 53 views
2

我有一個項目,其中Main-Method-class類的「Bootstrap.class」位於src/test文件夾中。所以我用<classpathScope>test</classpathScope> 能夠執行Maven exec plugin-如何在classpathscope = test時從main/java導入類

mvn exec:java 

我的問題:我得到一個「java.lang.IllegalArgumentException異常:未知實體:」造成I'm進口某些種類的,這是位於主/ JAVA 。關於如何解決這個問題的任何想法?

這是我的雙響炮:

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

堆棧跟蹤:

[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 
+1

看起來像hibernate配置錯誤 - 見[該相關(http://stackoverflow.com/questions/7887481/how-to-get-rid-of-java-lang- illegalargumentexception-unknown-entity-while-run)SO問題 – Raghuram 2012-04-04 11:41:45

+0

這也是我的第一個想法。但服務器和我的應用程序開始沒有任何問題。當我試圖將類「Bootstrap」(位於src/test中,並且包含一個main方法)作爲java應用程序運行時,纔會引發上述異常。 – slowessam 2012-04-04 12:22:15

+0

像@Raghuram說的,這看起來像一個Hibernate的conf問題 - 我的猜測是你沒有定義xml映射或應用java註釋到'eu.paedrtx.register.server.domain.PaymentDetails'類 – 2012-04-07 20:49:41

回答

0

我解決了這個問題。 問題是我有兩個persistence.xml,一個用於在測試/資源中測試,另一個用於主/資源中的應用。我必須刪除test/resources中的一個,並在應用程序persistence.xml中添加其他持久性單元。

看到:How to configure JPA for testing in Maven

0

嘗試使用類路徑參數如下描述http://mojo.codehaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html

此使用的,而不是javaexec目標將叉JVM,並且可能會修復很多問題,特別是使用Hibernate等工具進行非平凡的類加載。這是因爲Maven不必使用類加載器魔術來提供正確的類路徑進程(Maven是java進程本身,它是由簡單的類路徑啓動的,僅包含Maven引導程序的jar)。這樣它將只填充-cp參數,並分叉新過程:簡單易於診斷。

相關問題