【问题标题】:Force Maven compiler plugin to use generated sources instead of duplicate existing classes强制 Maven 编译器插件使用生成的源而不是重复现有的类
【发布时间】:2017-09-20 06:01:34
【问题描述】:

我正在为 Maven 开发一个插件。我需要替换源 Java 文件中的某些代码行(实际上是一个注释)。当然,我不会编辑原始文件,所以我会在我的插件中创建一个新文件。但我不能让 Maven 使用我生成的源而不是原始源。我试图在编译时排除一个文件,但它排除了生成的文件。没有排除我在编译项目时遇到重复的类错误。

感谢您的任何建议!

【问题讨论】:

  • 你在写什么样的注解?
  • @khmarbaise 目标之一是用 @Configurable 替换 Spring @Component 注释
  • 听起来有点奇怪...为什么不使用@Configurable 而不是@Component?除此之外,您的插件应该生成target/generated-sources 并设置getProject().addCompileSourceRoot( ); 以获取将源添加到将用于编译的文件夹的正确位置,当然您需要检查您的插件是否在正确的相位...
  • @khmarbaise 这个插件不会只替换注释:) 它将配置一个项目,以免给开发人员带来手动工作的负担。实际上,有一个 build-helper-maven-plugin 插件可以帮助将生成的源添加到正在处理的源中。问题是没有办法用生成的文件替换原始文件。我必须使用相同的类名。所以正如我所见,我必须使用字节码操作来替换注释。
  • build-helper-maven-plugin 是错误的方式......因为插件应该在没有任何用户补充配置的情况下处理所有这些......您只需在某处找到原始 java 文件除了src/main/javasomething like src/main/xxx,你的插件会选择它并在target/generated-sources/..中生成正确的类并将其添加到compileSourceRoot ....

标签: maven maven-2 maven-3 code-generation maven-plugin


【解决方案1】:

对不起,英语不是我的母语。 我能够让 Maven 使用我生成的源代码而不是原始源代码。

在我的项目中,我有基本配置的课程。以及带有客户端配置的目录。 在 Maven 构建期间,我需要用客户端替换基本配置类。我通过两个插件实现了这一点:

<build>
    <resources>
        <resource>
            <!-- Copy source file from another directory -->
            <directory>${project.basedir}/../configs/${client-directory}</directory>
            <targetPath>${project.basedir}/target/generated-sources/com/package</targetPath>
            <includes>
                <include>ClientConfig.java</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <excludes>
                    <!-- Exclude base config -->
                    <exclude>com/package/ClientConfig.java</exclude>
                </excludes>
                <compilerArgs>
                    <!-- Add client config -->
                    <arg>-sourcepath</arg>
                    <arg>${project.basedir}/target/generated-sources/</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

在包含和排除 maven-compiler-plugin 的标签中,您不能使用 java 文件的路径。您应该使用包和类名或模式。因此,您无法使用此标签将 java 文件从生成的源添加到编译。

你可以尝试使用 somethink 这样的。

【讨论】:

    猜你喜欢
    • 2021-01-03
    • 2013-11-07
    • 2013-06-02
    • 2011-06-03
    • 2012-11-21
    • 2020-03-25
    • 2018-06-09
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多