【问题标题】:How to enhance a class from a separate jar at build time with openJPA?如何在构建时使用 openJPA 增强来自单独 jar 的类?
【发布时间】:2015-08-26 14:37:32
【问题描述】:

我正在尝试使用 maven 插件 openjpa-maven-plugin 增强来自另一个 Jar 的 Entity 类,不幸的是我没有找到正确的方法。

我有一个类 MyPojo 来自模块 MyDomain 打包在 jar my-domain.jar 中:

public class MyPojo {

private Long id;

...

}

在我的第二个项目MyJpa 打包my-jpa.jar 中,它依赖于模块my-domain.jar,并且Maven 被配置为使用Build Time OpenJPA Enhancer,并具有以下功能:

<plugin>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <configuration>
                <includes>**/entity/*.class</includes>
                <addDefaultConstructor>true</addDefaultConstructor>
                <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
            </configuration>
            <executions>
                <execution>
                    <id>enhancer</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.openjpa</groupId>
                    <artifactId>openjpa</artifactId>
                    <version>2.3.0</version>
                </dependency>
            </dependencies>
        </plugin>

我正在使用在 persistence.xml 中声明的 XML 映射 orm.xml

            ... 
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>

    <mapping-file>META-INF/orm.xml</mapping-file>

 ...

并使用看起来像的 orm.xml:

    <entity class="MyPojo" access="FIELD">
    <table name="MYPOJO"/>
    <attributes>
        <id name="id">
            <generated-value strategy="AUTO"/>
        </id>
    </attributes>
    </entity>

运行mvn install 会出现以下错误:

此配置不允许运行时优化,但以下列出的类型在构建时或类加载时未使用 javaagent 增强:

当我将类 MyPojo 移动到项目 MyJpa(而不是项目 MyDomain)中时,它可以工作。

所以我的问题是:我需要配置什么才能在构建时增强来​​自外部 Jar 的类 MyPojo

【问题讨论】:

    标签: maven jpa openjpa openjpa-maven-plugin


    【解决方案1】:

    为了能够在构建时增强来​​自外部 Jar 的 MyPojo 类,我需要配置什么?

    如果我正确理解了您的问题,您将无法在构建时增强 jar 中的类。如果你想以这种方式增强,你需要解压缩类,增强,然后重新压缩。

    【讨论】:

    • 是的。那么有没有一种行家方式的方法呢?
    • 我不知道
    • 即使在同一个 Maven 构建中构建(并自行增强)另一个 JAR 作为 EAR 文件中的模块?
    猜你喜欢
    • 2021-06-02
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多