2

I am trying JAXB for xml mapping in a Maven project. I put JAXB jar in a separated project and add it as a dependency to my main project. I am using Eclipse.

Now the functionality of JAXB seems fine and from some XSD schema several Java classes are generated in target/generated-sources/xjc folder. The problem is Eclipse cannot resolve those classes in unit testing in main project. I even manually import those package name but still the class names are not resolvable. Is there anything I am missing?

dunfa
  • 509
  • 1
  • 10
  • 19
  • 1
    Is the directory ``target/generated-sources/xjc`` a java source folder in your project? – f1sh Apr 14 '16 at 15:59
  • @f1sh - The jaxb has its own project structure and the target/generated-sources/xjc is the default place JAXB puts all those classes in. Do I have to explicitly mark it as some source folder? And how? Also I notice that the jar from my jaxb-jar project is not in local .m2\repository even if I run the maven install on it. Could that be the problem? – dunfa Apr 14 '16 at 16:34
  • Since you use maven why don't you use [JAXB-2 Maven plugin](http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/example_xjc_basic.html) instead of using the jaxb jar in separate project? – vl4d1m1r4 Apr 14 '16 at 16:47
  • @vl4d1m1r4 - I am using the plugin. The "jaxb-jar" is the name of my project with those generated classes and I intend to use Maven install to create a jar from that project to use in my main project. – dunfa Apr 14 '16 at 16:54
  • 1
    In that case I personally would go with the multi module Maven project and add the "jaxb-jar" project as a dependency in the main project. Make sure that the packaging type of the "jaxb-jar" project is jar and that the folder where files are generated is added as source folder to the build path (Right click -> Build Path -> Use as source folder). You can also use the build-helper-maven-plugin to automate adding of source folder with the maven build. Hope this helps. – vl4d1m1r4 Apr 14 '16 at 17:17
  • @vl4d1m1r4 - Thanks. I think the Right click -> Build Path -> Use as source folder is what I need. Now I can use the generated classes. BTW, one of my team member confirm that he doesn't do that 'Use as source' explicitly but his workspace is fine. Is that something automatic/default in some case? Could you comment on that? And if you don't mind, you can put these stuff together as an answer and I will mark it as accepted. – dunfa Apr 14 '16 at 20:56

1 Answers1

7

You can add the generated source files to the build path by Right click -> Build Path -> Use as source folder on the folder with generated resources.

As stated in the comments the plugin you are using should automatically do this for you. In case it doesn't you can use the build-helper-maven-plugin (related question) with the following configuration:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>
      <id>add-source</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>target/generated-sources/xjc</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>
Community
  • 1
  • 1
vl4d1m1r4
  • 1,688
  • 12
  • 21