有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使cxfcodegenplugin生成的类能够持久化

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

在另一个使用该客户机的Maven项目中,只需使用JPA(当前使用OpenJPA),将cxf codegen插件生成的数据类(一些soap响应)的实例持久化即可

在编译/增强和安装客户机jar之前,在每次生成客户机源代码之后,使用一些配置工具(例如)可能会向数据类添加@Entity注释,但我希望在尽可能保持客户机通用性的同时,摆脱这个阶段。使用客户机的项目应该能够安全地假设类具有持久性

处理这个问题的最佳方法可能是在客户端项目设置中使用一些技巧(目前正在使用openjpa maven插件来增强数据类)来检测所需的类,并以某种方式使它们具有持久性功能,同时增强这些类

我宁愿跳过保养豆子之类的事情。xml和注释(如果可能的话),但这也是一种选择


共 (1) 个答案

  1. # 1 楼答案

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

    简而言之:我在我的^{中添加了以下内容

    <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>中。使用regexps可能会更时髦,但这对我来说已经足够好了