简体   繁体   中英

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.

Now on my other project adding this jar as a dependency with proper group id and artifactId.

But I am getting ClassNotFoundException and compile error for those generated jaxb classes.

I am updating my question to add more details.

The Pom File of Model Project.

<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. target jaxbclasses pqr xyz

with the package name - 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.

Now My Web project is not a Maven project - it is a Liferay based on Ant. I manually copy the A, B and The Jaxb Model project in to lib folder. It compile fines. but I am getting 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:

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

When I ran mvn dependency:list , I got this:

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.

This is most likely due to the fact that the target directory is never included in the jar file by default. Try configuring the JAXB classes to be generated under (say) target/generated . Then, add this to the build plugin section of the POM:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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