Skip to content

Maven 部署时包含源码

使用 mvn deploy 部署时,默认是不包含源码的。若想包含源码,只需以如下的方式添加 maven-source-plugin 插件的配置即可。

xml
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

需要注意的参数有两个:

  1. phase:这个是配置插件绑定到 maven 的哪个生命周期。

    一般设置为 verify ,这样可以保证 install 到本地和 deploy 到仓库时都包含源码。

    一个标准的生命周期是按照如下顺序执行的:

    • 验证 validate
    • 编译 compile
    • 测试 test
    • 打包 package
    • 检查 verify
    • 安装 install
    • 部署 deploy
  2. goal:用来配置在 phase 中绑定的生命周期结束后运行的 maven-source-plugin 插件的 goal

    需要注意的是,goal 在生命周期结束后执行

    比如若配置为 deploy ,此时虽然本地会生成 -sources.jar 文件,但仓库中却并没有上传。这是因为在执行部署处理时,maven-source-plugin 插件的 goal 还没有被执行,自然也就没法上传源码文件。

    maven-source-plugin 插件总共提供了 7 个 goal ,常用的是下面几个(其他的详见官方文档)。

    • source:jar is used to bundle the main sources of the project into a jar archive.
    • source:test-jar on the other hand, is used to bundle the test sources of the project into a jar archive.
    • source:jar-no-fork is similar to jar but does not fork the build lifecycle.
    • source:test-jar-no-fork is similar to test-jar but does not fork the build lifecycle.

    官方文档中推荐使用 jar-no-forktest-jar-no-fork

    另外,关于 jarjar-no-fork 的区别可以参考 StackOverflow 上的回复

附1. 库源与类 ClassName 的字节码不符

使用 lombok 组件时,由于生成的代码和实际的代码有差别,导致在打开此类源码文件时会有 库源与类 ClassName 的字节码不符 的提示。

找到有个 lombok-maven-plugin 的插件貌似可以解决这个问题。不过稍微试了下,貌似没啥效果。现在这样虽然有个错误消息,但也不影响使用,以后空了再调查。

参考

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.