简体   繁体   English

Maven生成的jaxb类-ClassNotFoundException

[英]maven generated jaxb classes - ClassNotFoundException

I have a maven model project, where I am generating jaxb class by maven command - clean install and the jaxb classes are generated under target folder and jar file is generating under .m2 repository folder. 我有一个Maven模型项目,在这里我通过maven命令生成jaxb类-干净安装,而jaxb类是在目标文件夹下生成的,而jar文件是在.m2存储库文件夹下生成的。

Now on my other project adding this jar as a dependency with proper group id and artifactId. 现在在我的另一个项目中,将此jar添加为具有适当组ID和artifactId的依赖项。

But I am getting ClassNotFoundException and compile error for those generated jaxb classes. 但是我正在收到ClassNotFoundException并为那些生成的jaxb类编译错误。

I am updating my question to add more details. 我正在更新我的问题以添加更多详细信息。

The Pom File of Model Project. 模型项目的Pom文件。

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <id>spf-ssp-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generateDirectory>${project.build.directory}/jaxbclasses/pqr/xyz</generateDirectory>
                            <generatePackage>abc.vo.apply.v1</generatePackage>
                            <schemaIncludes>
                                <include>MyXSD.xsd</include>
                            </schemaIncludes>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <schemaDirectory>src/main/resources</schemaDirectory>
                    <extension>true</extension>
                    <args>
                        <arg>-XtoString</arg>
                        <arg>-Xequals</arg>
                        <arg>-XhashCode</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.4</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>
        </plugins>
    </build>

On clean install it generates the class files under. clean install它会在下面生成类文件。 target jaxbclasses pqr xyz 目标jaxbclasses pqr xyz

with the package name - abc.vo.apply.v1 包名称为abc.vo.apply.v1

I have another Two Maven project(jar), suppose as, A & B. Now I can use the jaxb model project as a maven dependency, and it compile fine. 我还有另一个Maven项目(jar),假设是A和B。现在我可以将jaxb模型项目用作maven依赖项,并且可以很好地编译。

Now My Web project is not a Maven project - it is a Liferay based on Ant. 现在,我的Web项目不是Maven项目,而是基于Ant的Liferay。 I manually copy the A, B and The Jaxb Model project in to lib folder. 我手动将A,B和Jaxb Model项目复制到lib文件夹中。 It compile fines. 它会罚款。 but I am getting ClassNotFoundException. 但我收到ClassNotFoundException。

I am adding another answer, which I think is more accurate. 我要添加另一个答案,我认为这是更准确的。

In order to make your JAXB project compile, I had to add this dependency: 为了使您的JAXB项目编译,我必须添加此依赖项:

<dependency>
  <groupId>org.jvnet.jaxb2_commons</groupId>
  <artifactId>jaxb2-basics</artifactId>
  <version>0.6.5</version>
</dependency>

which obviously won't be automatically part of your classpath for Liferay. 这显然不会自动成为Liferay类路径的一部分。

When I ran mvn dependency:list , I got this: 当我运行mvn dependency:list ,我得到了:

org.jvnet.jaxb2_commons:jaxb2-basics-tools:jar:0.6.4:compile
org.jvnet.jaxb2_commons:jaxb2-basics:jar:0.6.4:compile
commons-lang:commons-lang:jar:2.2:compile
commons-logging:commons-logging:jar:1.1.1:compile
com.google.code.javaparser:javaparser:jar:1.0.8:compile
org.jvnet.jaxb2_commons:jaxb2-basics-runtime:jar:0.6.4:compile
commons-beanutils:commons-beanutils:jar:1.7.0:compile`

which means that you need to put these in the lib directory of your Liferay installation as well. 这意味着您还需要将它们放在Liferay安装的lib目录中。

This is most likely due to the fact that the target directory is never included in the jar file by default. 这很可能是由于默认情况下target目录从不包含在jar文件中。 Try configuring the JAXB classes to be generated under (say) target/generated . 尝试将JAXB类配置为在(例如) target/generated Then, add this to the build plugin section of the POM: 然后,将其添加到POM的build plugin部分:

  <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>build-helper-maven-plugin</artifactId>
     <version>1.9.1</version>
     <executions>
        <execution>
          <id>add-java-sources</id>
          <goals>
            <goal>add-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>${project.build.directory}/generated</source>
            </sources>
          </configuration>
       </execution>
    </executions>
  </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM