1

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.

Manu
  • 1,071
  • 4
  • 15
  • 34

2 Answers2

2

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.

Daniel
  • 3,572
  • 4
  • 24
  • 31
  • Awesome Daniel, You are amazing, it is working now after put those jars in my liferay project. – Manu Sep 14 '15 at 12:56
  • No problem, just glad I could help out. If you are satisfied with this answer, please consider marking it as 'correct'. – Daniel Sep 14 '15 at 16:09
  • Hi Daniel,By using following jaxb args -XtoString -Xequals -XhashCode classes can implement public class SPFSSPVO implements Equals, HashCode, ToString, help meI how the jaxb classes can implements Serializable interface. – Manu Sep 15 '15 at 05:15
  • That's really a separate question. Basically, you need something called a "bindings file" that the JAXB plugin needs to use. [This](http://stackoverflow.com/questions/1513972/how-to-generate-a-java-class-which-implements-serializable-interface-from-xsd-us) and [this](http://an-compsci.blogspot.se/2012/03/configuring-maven-to-autogenerate.html) article describe how it's done. – Daniel Sep 15 '15 at 07:23
1

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>
Daniel
  • 3,572
  • 4
  • 24
  • 31
  • [tag:maven-jaxb2-plugin] adds the "generated" directory as a source directory automatically. – lexicore Aug 21 '15 at 13:22
  • I agree, that's the default. But unless the OP posts the output from execution or the POM, we are stuck with making qualified guesses. Logically, **if** the plugin did indeed generate the code **and** automatically added the generated directory as a source folder, the code should be in the jar. Moreover, the OP could be using `org.codehaus.mojo:jaxb2-maven-plugin` for all we know. @Manu: could you please add more information to this post? We'd like to help out, but we need more context. – Daniel Aug 24 '15 at 06:30
  • This is why I've posted it as a comment to your answer (which is points to the right direction) and not as a separate answer. – lexicore Aug 24 '15 at 07:08
  • @Daniel and, I have updated the question, to add some more details. – Manu Sep 07 '15 at 12:40
  • I tried using your POM and sure enough, the classes are duly included in the jar-file. Are you declaring your dependencies correctly? How are you accessing the generated classes (by reflection or statically)? They were generated under under the `abc.vo.apply.v1` package, as expected. When you say "manually copy the A, B and The Jaxb Model project in to lib folder", you do mean the jar-files, right? – Daniel Sep 08 '15 at 07:10
  • Hi Daniel, I am not using the jaxb classes, by Reflection. this is my import -import abc.vo.apply.v1.SPFSSPVO; Yes Daniel manually copy means - the three jar files copying to Lib folder. – Manu Sep 09 '15 at 09:12
  • Ok, does the exception actually say `java.lang.ClassNotFoundException: abc.vo.apply.v1.SPFSSPVO` or is it some other class? Are there any nested exceptions? – Daniel Sep 10 '15 at 14:58