【问题标题】:Make cxf-codegen-plugin generated class persistence capable使 cxf-codegen-plugin 生成的类能够持久化
【发布时间】:2017-06-16 12:21:26
【问题描述】:

我有一个使用 cxf-codegen-plugin 创建 SOAP 客户端的 Maven jar 项目。

在另一个使用该客户端的 Maven 项目中,只需要使用 JPA (当前使用 OpenJPA).

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

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

如果可能的话,我宁愿跳过维护 beans.xml 之类的东西并坚持使用注释,但这也是一种选择。

【问题讨论】:

    标签: java maven jpa soap cxf-codegen-plugin


    【解决方案1】:

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

    很快:我在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>
    

    为了保持代码格式良好,&lt;replacement&gt;s 中需要所有缩进和换行符。使用正则表达式可能会更时尚,但这对我来说已经足够了。

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 2020-12-02
      • 2021-10-10
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多