如何在Maven编译期间忽略Java Source目录? [英] How to ignore the Java Source directory during Maven Compilation?

查看:254
本文介绍了如何在Maven编译期间忽略Java Source目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Lombok Maven插件使用 Lombok 时确保正确创建Javadoc。



Lombok Maven在编译之前引入了新的代码生成目标。在我的配置中,我的 sourceDirectory (带有Lombok注释的Java, src / main / java )被处理以创建Java( target / generated-sources / delombok



然而,<$ c $中的每个文件都没有Lombok注释c> sourceDirectory 现在在 target / generated-sources / delombok 中有一个相应的(同名)文件,导致由于重复的类导致编译失败。



如何告诉Java编译器忽略 sourceDirectory 中的来源?



请注意,默认的Lombok Maven配置会让开发人员将Java(带有Lombok注释)放在 src / main / lombok 文件夹而不是 src / main / java 。但是,我不希望这样做,因为它会混淆IDE并且我的代码编译得很好(如果我删除了Maven插件)。



另请注意,只需重新定义 sourceDirectory 也会扰乱IDE(他们不知道在哪里可以找到Java源代码!)。

解决方案

我最近改用了使用flakey maven-exec-plugin方法为javadoc工具生成原始资源以使用lombok-maven-plugin



My设置




  • src / main / java
  • $ b中的所有来源$ b
  • 生成的资源包含在 target / generated-sources / delombok



<我最初遇到了这个问题,但它似乎是一个简单的修复:不要让lombok-maven-plugin使用 addOutputDirectoy 将delombok路径添加到编译器源路径。 IE

 < plugin> 
< groupId> org.projectlombok< / groupId>
< artifactId> lombok-maven-plugin< / artifactId>
< version> 0.11.2.0< / version>
< executions>
< execution>
< phase> generate-sources< / phase>
< goals>
< goal> delombok< / goal>
< / goals>
< / execution>
< / executions>
< configuration>
< addOutputDirectory> false< / addOutputDirectory>
< sourceDirectory> src / main / java< / sourceDirectory>
< / configuration>
< / plugin>

这似乎解决了现在的问题



编辑:奖励,如何使用此设置生成正确的javadoc

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-javadoc-plugin< / artifactId>
< version> 2.8.1< / version>
< configuration>
< defaultVersion> $ {project.version}< / defaultVersion>
< sourcepath> target / generated-sources / delombok< / sourcepath>
< / configuration>
< / plugin>


I am trying to use the Lombok Maven Plugin to ensure the correct creation of Javadocs when using Lombok.

Lombok Maven introduces a new code generation goal, just prior to compilation. In my configuration, my sourceDirectory (Java with Lombok annotations, src/main/java) is processed to create Java (without Lombok annotations) in target/generated-sources/delombok.

However, every file in sourceDirectory now has a corresponding (identically named) file in target/generated-sources/delombok, resulting in compilation failures due to duplicate classes.

How can I tell the Java compiler to ignore the sources in sourceDirectory?

Note that the default Lombok Maven configuration would have the developer put Java (with Lombok annotations) in the src/main/lombok folder instead of src/main/java. However, I do not wish to do this because it confuses IDEs and my code compiles just fine (if I remove the Maven plugin).

Also note that simply redefining sourceDirectory will also upset IDEs (they no longer know where to find the Java source code!).

解决方案

I recently switched from using the flakey maven-exec-plugin approach to generate raw sources for the javadoc tool to using lombok-maven-plugin

My setup

  • All sources in src/main/java
  • Generated sources go in target/generated-sources/delombok

I initially ran into this problem but it seems to be an easy fix: Don't let lombok-maven-plugin add the delombok path to the compiler source paths with addOutputDirectoy. IE

<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>0.11.2.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <addOutputDirectory>false</addOutputDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
    </configuration>
</plugin>

This seems to of solved the issue for now

EDIT: Bonus, how to generate proper javadocs with this setup

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.8.1</version>
    <configuration>
        <defaultVersion>${project.version}</defaultVersion>
        <sourcepath>target/generated-sources/delombok</sourcepath>
    </configuration>
</plugin>

这篇关于如何在Maven编译期间忽略Java Source目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆