【问题标题】:Create and install de-lomboked source jar in maven在 maven 中创建和安装 de-lomboked 源 jar
【发布时间】:2018-09-17 07:05:34
【问题描述】:

免责声明:我已经解决了这个问题,并且正在记录解决方案,让全世界都知道。

如何在 maven 中创建和安装包含“delomboked”源代码的 *-sources.jar?

默认情况下,maven-source-plugin 会创建一个源 jar,而不会对源文件进行分解,这会导致依赖于库二进制文件的项目抱怨源文件不匹配。

【问题讨论】:

  • 如果您的项目使用 lombok,您必须将其作为依赖项。但是,您为什么要尝试 delombok 生成的 -source 包?我不明白你的想法或问题?
  • @khmarbaise 当您尝试调试使用您使用 lombok 创建的库的代码时,会出现问题,运行时具有已编译(因此是 delomboked)的类文件,但源 jar 将具有原始 lombok带注释的源文件。您的 IDE 会抱怨 .java 源文件和 .class 文件不匹配,这使得调试变得困难。个人喜欢在源代码中下断点,让IDE与编译后的代码相匹配,但是当编译的lombok代码在java源文件中尚不存在时,这并不容易。

标签: java maven lombok


【解决方案1】:

TL;DR(下面解释)

在 pom.xml 的 project.build 元素中将以下插件配置添加到 plugins 配置中

<project>
    ...
<build>
<plugins>
    ...
<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.18.0.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <sourceDirectory>src/main/java</sourceDirectory>
        <outputDirectory>${project.build.directory}/delombok</outputDirectory>
        <addOutputDirectory>false</addOutputDirectory>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>copy-to-lombok-build</id>
            <phase>process-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>${project.basedir}/src/main/resources</directory>
                    </resource>
                </resources>
                <outputDirectory>${project.build.directory}/delombok</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>generate-delomboked-sources-jar</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <jar destfile="${project.build.directory}/${project.build.finalName}-sources.jar"
                         basedir="${project.build.directory}/delombok"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
        <execution>
            <id>install-source-jar</id>
            <goals>
                <goal>install-file</goal>
            </goals>
            <phase>install</phase>
            <configuration>
                <file>${project.build.directory}/${project.build.finalName}-sources.jar</file>
                <groupId>${project.groupId}</groupId>
                <artifactId>${project.artifactId}</artifactId>
                <version>${project.version}</version>
                <classifier>sources</classifier>
                <generatePom>true</generatePom>
                <pomFile>${project.basedir}/pom.xml</pomFile>
            </configuration>
        </execution>
    </executions>
</plugin>
</plugins>
</build>
</project>

说明

lombok-maven-plugin 将使您能够删除源代码 (${project.basedir}/src/main/java) 并将其放置在目标目录 (${project.build.directory}/delombok) 中。通常这会将代码放在${project.build.directory}/generated-sources/delombok 文件夹中,但是由于 Intellij 会自动考虑这个额外的源代码,所以在开发你的库时会出现重复代码错误,为了阻止这种情况,只需指定一个非默认目标目录(在这个案例就在generated-sources 目录之外)。

maven-resources-plugin 是必要的,以便还从标准 ${project.basedir}/src/main/resources 目录复制资源。如果您的项目中还有其他非标准资源目录,您应该在此插件的资源部分配置它们。

maven-antrun-plugin 用于代替 maven-source-plugin,因为您不能在后面指定自定义源目录。 jar 任务指向我们自定义的“生成源”并生成标准命名的源 jar。

maven-install-plugin 使用install-file 目标是因为您无法使用install 目标附加罐子。我们可以通过使用install-file 目标和sources 分类器手动安装文件来破解解决方案。

我希望这可以帮助那些像我一样在奋斗路上遇到这个问题的人。

【讨论】:

    【解决方案2】:

    以下解决方案基于上面提供的解决方案,但通过使用 build-helper 插件来附加生成的 delomboked 源 jar 而不是使用 install-file 对其进行了改进。这样做的好处是,正常的 maven 安装和部署阶段可以正确处理生成的文件,就像使用源插件时一样。

    <project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-maven-plugin</artifactId>
                <version>1.18.12.0</version>
                <executions>
                    <execution>
                        <id>delombok-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>src/main/java</sourceDirectory>
                            <outputDirectory>${project.build.directory}/delombok</outputDirectory>
                            <addOutputDirectory>false</addOutputDirectory>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>generate-delomboked-sources-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <jar destfile="${project.build.directory}/${project.build.finalName}-sources.jar"
                                     basedir="${project.build.directory}/delombok"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>attach-delomboked-sources-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>${project.build.directory}/${project.build.finalName}-sources.jar</file>
                                    <type>jar</type>
                                    <classifier>sources</classifier>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    </project>
    

    【讨论】:

    • 嗨!欢迎来到 SO!切记不要使用答案来表示感谢或提供与回答问题没有直接关系的信息! stackoverflow.com/help/how-to-answer
    • @CésarLandesa 我已经编辑了这篇文章,以更清楚地表明它是一个与原始问题直接相关的改进解决方案。如果您的否决不是由于技术上的分歧,请您将其删除吗?否则,您错误地向正在寻找此问题解决方案的其他人表明我的回答有问题。
    【解决方案3】:

    想指出,也可以使用配置文件(绕过不可自定义的构建源目录)。解决方案描述于https://sudonull.com/post/1197-Lombok-sourcesjar-and-convenient-debug

    将以下内容添加到 pom.xml 属性:

    <origSourceDir>${project.basedir}/src/main/java</origSourceDir>
    <sourceDir>${origSourceDir}</sourceDir>
    <delombokedSourceDir>${project.build.directory}/delombok</delombokedSourceDir>
    </properties>
    

    配置文件和构建部分更改:

    <profiles>
        <profile>
            <id>build</id>
            <properties>
                <sourceDir>${delombokedSourceDir}</sourceDir>
            </properties>
        </profile>
    </profiles>
    <build>
        <sourceDirectory>${sourceDir}</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-maven-plugin</artifactId>
                <version>1.18.20.0</version>
                <executions>
                    <execution>
                        <id>delombok</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                        <configuration>
                            <addOutputDirectory>false</addOutputDirectory>
                            <sourceDirectory>${origSourceDir}</sourceDirectory>
                            <outputDirectory>${delombokedSourceDir}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    使用mvn clean install -Pbuild执行

    这应该可以解决 IntelliJ 中的“库源与类的字节码不匹配”错误,并在大多数情况下允许无缝调试。

    参考:“Delombok plugin + profile in maven”

    【讨论】:

    • 感谢您的解决方案。我在maven-source-plugin 之后添加了一个 Ant 任务,以便删除 delomboked 源,因为 Intellij IDEA 默认自动将此目录添加为源文件夹:&lt;plugin&gt;&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;&lt;executions&gt;&lt;execution&gt;&lt;phase&gt;verify&lt;/phase&gt;&lt;goals&gt;&lt;goal&gt;run&lt;/goal&gt;&lt;/goals&gt;&lt;configuration&gt;&lt;target&gt;&lt;delete includeEmptyDirs="true"&gt;&lt;fileset dir="${delombokedSourceDir}" /&gt;&lt;/delete&gt;&lt;/target&gt;&lt;/configuration&gt;&lt;/execution&gt;&lt;/executions&gt;&lt;/plugin&gt;
    【解决方案4】:

    对于多模块项目或纯 pom 项目,这两个答案都是有缺陷的,因为没有源,所以你必须创建一个空目录,它会生成一个空的 .jar。

    有一种简单(但更复杂)的方法可以实现您想要的功能:制作您自己的Maven Plugin。

    听起来过于复杂,但我们可以重复使用 maven-sources-plugin 并配合 MOJO 的继承更新必要部分:

    import java.util.List;
    import java.util.stream.Collectors;
    import org.apache.maven.plugins.annotations.LifecyclePhase;
    import org.apache.maven.plugins.annotations.Mojo;
    import org.apache.maven.plugins.annotations.Parameter;
    import org.apache.maven.plugins.source.SourceJarNoForkMojo;
    import org.apache.maven.project.MavenProject;
    
    /**
     * This goal bundles all the sources into a jar archive, but uses delomboked sources.
     */
    @Mojo(name = "jar-no-fork", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
    public class EnhancedSourceJarNoForkMojo extends SourceJarNoForkMojo {
    
        @Parameter(property = "<some-prefix>.useDelombokSources", defaultValue = "true")
        protected boolean useDelombokSources;
    
        @Parameter(property = "<some-prefix>.delombokSourcesLocation", defaultValue = "delombok")
        protected String delombokSourcesLocation;
    
        @Override
        protected List<String> getSources(MavenProject p) {
            // if user doesn't want delomboked sources, use default algorithm
            List<String> sources = super.getSources(p);
            if (!useDelombokSources) {
                return sources;
            }
    
            // typically, sources' list will contain: [src/main/java, target/generated_sources].
            // replace src/main/java if it's present with delombok-generated sources
            String target = p.getBuild().getDirectory();
            return super.getSources(p)
                    .stream()
                    .map(s -> s.endsWith("java") ? String.format("%s/%s", target, delombokSourcesLocation) : s)
                    .collect(Collectors.toList());
        }
    
    }
    

    pom.xml 例程的要点是 available here。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2014-06-18
      • 2016-01-09
      • 2021-03-24
      相关资源
      最近更新 更多