2017-02-21 45 views
14

在Maven Build中,我使用字节代码生成库(Byte Buddy)动态生成一些Java类型。自然,这些类文件没有相应的源文件。这种方式只会产生几个班级。该项目的大部分代码将成为Java源代码。理想情况下,Java源代码将以静态方式引用生成的类型,而不是使用反射或运行时代码生成,这意味着类需要位于javac的编译类路径上。我是否可以在相同Maven项目的编译类路径上获得生成的类,即不需要单独的Maven项目和工件来保存包含源代码的Maven项目所引用的生成的字节码?我可以在同一个Maven项目中动态生成和引用源文件中的类吗?

更新:我已经试图把生成的类直接进入target/classesproject.build.outputDirectory,早在Maven构建生命周期,但似乎不是在类路径上。生成的类型无法通过 Maven编译器插件或 IDE解决。我也尝试使用Build Helper Maven Plugin在target下添加一个额外的资源目录,其中包含生成的类,然后将其自动复制到target/classes。这种配置表现出同样的问题。

更新:我已经创建了一个完整的公共回购在GitHub上:https://github.com/mches/so-42376851

这里是Maven的POM,因为我想有一个参考的字节码增强类静态类项目:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
     <groupId>demo</groupId> 
     <artifactId>demo</artifactId> 
     <version>1.0-SNAPSHOT</version> 
     <relativePath>../pom.xml</relativePath> 
    </parent> 

    <artifactId>demo-enhanced</artifactId> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <executions> 
        <execution> 
         <phase>generate-resources</phase> 
         <goals> 
          <goal>unpack</goal> 
         </goals> 
         <configuration> 
          <artifactItems> 
           <artifactItem> 
            <groupId>demo</groupId> 
            <artifactId>demo-original</artifactId> 
            <version>${project.version}</version> 
            <overWrite>true</overWrite> 
            <outputDirectory>${project.build.outputDirectory}</outputDirectory> 
            <includes>**/*.class</includes> 
            <excludes>META-INF/</excludes> 
           </artifactItem> 
          </artifactItems> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>net.bytebuddy</groupId> 
       <artifactId>byte-buddy-maven-plugin</artifactId> 
       <executions> 
        <execution> 
         <phase>process-resources</phase> 
         <goals> 
          <goal>transform</goal> 
         </goals> 
         <configuration> 
          <initialization> 
           <entryPoint>net.bytebuddy.test.SimpleEntryPoint</entryPoint> 
           <groupId>demo</groupId> 
           <artifactId>demo-transformer</artifactId> 
          </initialization> 
          <transformations> 
           <transformation> 
            <plugin>net.bytebuddy.test.SimplePlugin</plugin> 
            <groupId>demo</groupId> 
            <artifactId>demo-transformer</artifactId> 
           </transformation> 
          </transformations> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

这里是父POM:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>demo</groupId> 
    <artifactId>demo</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 

    <modules> 
     <module>demo-original</module> 
     <module>demo-transformer</module> 
     <module>demo-enhanced</module> 
    </modules> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 
     <byte-buddy.version>1.6.9</byte-buddy.version> 
    </properties> 

    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>net.bytebuddy</groupId> 
       <artifactId>byte-buddy</artifactId> 
       <version>${byte-buddy.version}</version> 
      </dependency> 
      <dependency> 
       <groupId>demo</groupId> 
       <artifactId>demo-original</artifactId> 
       <version>${project.version}</version> 
      </dependency> 
      <dependency> 
       <groupId>demo</groupId> 
       <artifactId>demo-transformer</artifactId> 
       <version>${project.version}</version> 
      </dependency> 
      <dependency> 
       <groupId>demo</groupId> 
       <artifactId>demo-enhanced</artifactId> 
       <version>${project.version}</version> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <build> 
     <pluginManagement> 
      <plugins> 
       <plugin> 
        <groupId>net.bytebuddy</groupId> 
        <artifactId>byte-buddy-maven-plugin</artifactId> 
        <version>${byte-buddy.version}</version> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
    </build> 

</project> 

富/ Bar.java(原始源) :

package foo; 

public class Bar { 
} 

净/ bytebuddy /测试/ SimplePlugin.java(字节代码增强剂):

package net.bytebuddy.test; 

import net.bytebuddy.build.Plugin; 
import net.bytebuddy.description.modifier.Visibility; 


import net.bytebuddy.description.type.TypeDescription; 
import net.bytebuddy.dynamic.DynamicType; 
import net.bytebuddy.implementation.FieldAccessor; 

public class SimplePlugin implements Plugin { 
    @Override 
    public boolean matches(TypeDescription target) { 
     return target.getName().equals("foo.Bar"); 
    } 

    @Override 
    public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription) { 
     if (typeDescription.getTypeName().equals("foo.Bar")) { 
      builder = builder.defineField("qux", String.class, Visibility.PRIVATE) 
        .defineMethod("getQux", String.class, Visibility.PUBLIC) 
        .intercept(FieldAccessor.ofField("qux")) 
        .defineMethod("setQux", void.class, Visibility.PUBLIC) 
        .withParameter(String.class) 
        .intercept(FieldAccessor.ofField("qux")); 
     } 
     return builder; 
    } 
} 

富/ Baz.java(静态源文件引用动态型):

package foo; 

public class Baz { 
    private Bar bar = new Bar(); 

    public String getQuux() { 
     return bar.getQux(); 
    } 

    public void setQuux(String quux) { 
     bar.setQux(quux); 
    } 
} 

更新: Maven似乎理解一个结构涉及整合模块与增强字节码和静态类源代码,一个以及每个模块的独立模块,但IDE,IntelliJ和Eclipse无法理解Maven的结构类路径。

+0

也许你可以看看jax-b的源代码:xjc它不会simil关于生成源代码阶段的东西 – HRgiger

回答

1

是的,你可以。在预编译阶段生成类,并将其放入目标/类文件夹。

+0

我曾尝试在Maven Build Lifecycle中放置类,但似乎'target/classes'('project.build.outputDirectory')不在类路径中。 Maven编译器插件或IDE无法解析生成的类型。 –

+1

@Mark它们位于类路径中。你在什么阶段将它们生成为“目标/类”?你确定它们是用正确的包装结构生成的吗?您是否在日志中或在该文件夹中生成了? – Tunaki

+0

@Tunaki我在'generate-resources'阶段将未增强的类提取到'target/classes'。我在“流程资源”阶段增强了这些类。你说得对。用'--debug'运行Maven显示'classpathElements'包含'target/classes'。使用我当前的配置,我可以让Maven编译引用'target/classes'中存在的生成类型的静态类,并且我可以看到该类是正确的,但IDE(IntelliJ和Eclipse)不似乎识别静态类型中生成的类和报告源级错误(无法解析符号)。 –

4

借助Byte Buddy,您正在生成类文件而不是源文件。不幸的是,Maven只知道生成的源文件,而不知道生成的类文件。因此最简单的就是创建一个特定的模块。

如果这不可行,您可以将它们复制到Maven项目的任何源文件夹,例如resources。一些IDE会找到这些类并对待它们,只要它们在java文件夹中,但没有试图编译它们,因为文件以.class结尾。

+0

看来Maven理解这种结构,但IDE,IntelliJ和Eclipse,却无法理解它。我发现创建一个额外的模块,将静态类(依赖于具有增强类的模块)放在我的面前并没有改善IDE以Maven理解它的方式理解类路径的能力。 –

1

这不是我的理想的解决方案,但似乎工作的想法是做两个让步:为提高字节码

  1. 使用单独的Maven模块和静态类。
  2. 不要在与静态类模块相同的Maven reactor构建中构建增强的字节码模块。

,为什么这似乎工作:

在IDE中生成自己的项目/模块配置,基于Maven的pom.xml文件,他们会在依赖带来增强的字节代码作为库/ JAR而不是项目/模块。 JAR在静态类模块的IDE类路径上结束,并且解析了增强类。

用于增强的字节代码模块的IntelliJ项目文件:

<?xml version="1.0" encoding="UTF-8"?> 
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> 
    <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false"> 
    <output url="file://$MODULE_DIR$/target/classes" /> 
    <output-test url="file://$MODULE_DIR$/target/test-classes" /> 
    <content url="file://$MODULE_DIR$"> 
     <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> 
     <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> 
     <excludeFolder url="file://$MODULE_DIR$/target" /> 
    </content> 
    <orderEntry type="inheritedJdk" /> 
    <orderEntry type="sourceFolder" forTests="false" /> 
    </component> 
</module> 

在单独的Maven模块的IntelliJ项目文件构建(错误):与Maven模块在相同的积累

<?xml version="1.0" encoding="UTF-8"?> 
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> 
    <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false"> 
    <output url="file://$MODULE_DIR$/target/classes" /> 
    <output-test url="file://$MODULE_DIR$/target/test-classes" /> 
    <content url="file://$MODULE_DIR$"> 
     <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> 
     <excludeFolder url="file://$MODULE_DIR$/target" /> 
    </content> 
    <orderEntry type="inheritedJdk" /> 
    <orderEntry type="sourceFolder" forTests="false" /> 
    <orderEntry type="module" module-name="demo-enhanced" /> 
    </component> 
</module> 

的IntelliJ项目文件(没有错误):

<?xml version="1.0" encoding="UTF-8"?> 
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> 
    <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false"> 
    <output url="file://$MODULE_DIR$/target/classes" /> 
    <output-test url="file://$MODULE_DIR$/target/test-classes" /> 
    <content url="file://$MODULE_DIR$"> 
     <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> 
     <excludeFolder url="file://$MODULE_DIR$/target" /> 
    </content> 
    <orderEntry type="inheritedJdk" /> 
    <orderEntry type="sourceFolder" forTests="false" /> 
    <orderEntry type="library" name="Maven: demo:demo-enhanced:1.0-SNAPSHOT" level="project" /> 
    </component> 
</module> 
相关问题