使cxf-codegen-plugin生成的类持久性有效 [英] Make cxf-codegen-plugin generated class persistence capable

查看:131
本文介绍了使cxf-codegen-plugin生成的类持久性有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven jar项目,它使用cxf-codegen-plugin创建了一个SOAP客户端.

I have a Maven jar project that creates a SOAP client using cxf-codegen-plugin.

在另一个使用该客户端的Maven项目中,仅需要持久保存数据类(某些肥皂响应)的实例-由cxf-codegen-plugin生成-使用JPA (当前使用OpenJPA).

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

可能有一些配置方面的东西-例如,在每个客户端源代码生成之后,在编译/增强和安装客户端jar之前将@Entity注释添加到数据类中,但是我想摆脱此阶段,仍保持客户端尽可能通用.使用客户端的项目应该只能够安全地假定该类具有持久性.

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.

解决此问题的最佳方法可能是客户端项目设置(目前使用openjpa-maven-plugin来增强数据类)中的一些技巧,以检测所需的类并以某种方式使它们具有持久性并增强它们.

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.

我宁愿跳过诸如维护bean.xml之类的内容,并尽可能地坚持注释,但这也是一个选择.

I'd rather skip stuff like maintaining beans.xml and stick to the annotations if possible but it is an option too.

推荐答案

如果有人需要同样的方法,我将描述我当前使用的方法.它基于使用com.google.code.maven-replacer-plugin添加注释,idimports之类的字段.

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.

简而言之:我在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>

要使代码格式正确,必须在<replacement>中使用所有缩进和换行符.使用正则表达式可能会更时尚,但这对我来说已经足够了.

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.

这篇关于使cxf-codegen-plugin生成的类持久性有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆