问题
I have a Maven jar project that creates a SOAP client using cxf-codegen-plugin.
In an another Maven project using that client it is simply needed to persist instance of a data class (some soap response) - generated by cxf-codegen-plugin - with JPA (currently using OpenJPA).
It might be possible with some configuration stuff - for example - after each client source code generation to add @Entity annotation to data class before compiling/enhancing and installing the client jar but I'd like to get rid of this phase while still keeping the client as generic as possible. Projects using client should just be able to safely assume that the class is persistence capable.
Best way to handle this might be some tricks in client project settings (currently using openjpa-maven-plugin for enhancing data classes) to detect desired classes and somehow make them persistence capable plus enhance those.
I'd rather skip stuff like maintaining beans.xml and stick to the annotations if possible but it is an option too.
回答1:
In case someone needs the same i describe the method i currently use. It is based on adding annotations, fields like id
and imports
using com.google.code.maven-replacer-plugin
.
Shortly: i have added following stuff in my pom.xml
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- dir where cxf-codegen-plugin has generated model classes -->
<basedir>src/generated/java/org/example/service</basedir>
<includes>
<include>NamedEntity.java</include>
</includes>
<regex>false</regex>
<replacements>
<replacement>
<token>package org.example.service.service;</token>
<value>package org.example.service.service;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.GeneratedValue;
import javax.persistence.InheritanceType;
</value>
</replacement>
<replacement>
<token>public class</token>
<value>@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class</value>
</replacement>
<replacement>
<token>protected String name;
</token>
<value>protected String name;
@Id
@GeneratedValue
@Getter
private Long id;
</value>
</replacement>
</replacements>
</configuration>
</plugin>
To keep the code nicely formatted all indents and line feeds are required in <replacement>
s. Using regexps this might be done more stylish but this is good enough for me.
来源:https://stackoverflow.com/questions/44589147/make-cxf-codegen-plugin-generated-class-persistence-capable