简体   繁体   中英

Generate java classes with JAXB2 in another maven project

Can someone please help me with my problem?

I have two maven projects A and B. I want project A to contain all my model classes and in project B i'm creating a contract-first web service with Spring WS. So in project B, I use maven-jaxb2-plugin to generate classes from my schema. It happens that the generate classes in my webservice project (project A) are identical to the ones in my model project (project A) (with no XML annotations).

Because i don't want to have duplicate classes in my web service project (project B), i decided to make this project depends on the model project (project A) and what i want next is, not anymore generate classes to the webservice project but to the model project (project A).

Do you think there isn't another way to do this ? Can someone please help me doing this if it's possible?

Project A

package project.a;

public class Client {
  //...
}


Project B

Class

package project.b;

public class Compte {
  //This class manipulates a Client object
  //...
}


JAXB2 Maven Plugin

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <id>xjc</id>
      <goals>
        <goal>xjc</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <outputDirectory>src/main/java</outputDirectory>
    <packageName>project.a</packageName>
    <schemaDirectory>src/main/webapp/WEB-INF/schemas</schemaDirectory>
    <clearOutputDir>false</clearOutputDir>
  </configuration>
<plugin>


Schema

<element name="client">
  <complexType>
    <sequence>
      <!--  -->
    </sequence>
  </complexType>
</element>

Project B spring bean configuration

<bean class="org.springframework.ws.client.core.WebServiceTemplate">
  <property name="marshaller" ref="marshaller"/>
  <property name="unmarshaller" ref="marshaller"/>
  <property name="defaultUri" value="http://localhost:8080/project/" />
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="contextPath" value="project.a"/>
</bean>

You don't have to recreate model classes again. Just place them in Project A and then import those beans into Project B as:

    <import resource="classpath:projectABeanDefinitionFile.xml" />

Assuming you are implementing/going to @XmlRootElement on Compte class (else suggest to go through a tutorial like here ), then change project B file as:

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
      <property name="classesToBeBound" value="project.b.Compte"/>
    </bean>

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