You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven源码生成异常及自动打包配置问题求助

Fixing Maven Source JAR Auto-Generation for Package/Install Goals

Let's get your Maven source plugin working reliably so you don't have to manually add source:jar to your commands—perfect for CI tools that can't tweak the target list. You already sorted out the local repo sync issue by reordering the command, so let's focus on getting the pom configuration back on track.

What's Likely Wrong With Your Current Config

Your existing plugin setup has a few potential pitfalls that might be causing it to fail silently:

  • Custom properties like ${source.directory} or ${source.classifier} might not be defined in your pom, leaving the plugin without valid paths/names
  • Using the jar goal instead of the more efficient jar-no-fork (which reuses existing compiled files instead of re-compiling)
  • Missing a plugin version (Maven defaults to an older version sometimes, which can have compatibility issues)

Corrected Plugin Configuration

Here's an adjusted version of the plugin setup that should work consistently. I'll include notes for customizations if you need them:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.3.0</version> <!-- Always specify a stable, recent version -->
    <executions>
        <execution>
            <id>attach-sources</id>
            <phase>package</phase> <!-- Matches your request to generate on package -->
            <goals>
                <goal>jar-no-fork</goal> <!-- Faster, no redundant compilation -->
            </goals>
            <configuration>
                <!-- Use Maven's built-in target directory property if you don't need a custom path -->
                <outputDirectory>${project.build.directory}</outputDirectory>
                <!-- If you want a custom final name, define ${project.jar.name} in your pom's <properties> first -->
                <!-- <finalName>${project.jar.name}</finalName> -->
                <!-- Stick to Maven's standard classifier for sources to avoid confusion -->
                <classifier>sources</classifier>
                <!-- Remove the fileNameMapping if you want the version included (standard behavior) -->
                <!-- <fileNameMapping>no-version</fileNameMapping> -->
            </configuration>
        </execution>
    </executions>
</plugin>

Key Adjustments Explained

  1. Plugin Version: Pinning the version avoids unexpected behavior from outdated default versions. 3.3.0 is a stable, recent release at the time of writing.
  2. jar-no-fork Goal: This skips re-compiling your code, making the build faster and avoiding any compilation-related issues that might break source JAR generation.
  3. Standard Properties: Using ${project.build.directory} (which maps to your target folder) ensures the plugin always knows where to output the JAR, no extra property setup needed.
  4. Classifier: Using the standard sources classifier ensures your source JAR is named like module-sources.jar—matching what you get when running source:jar manually, and ensuring Maven recognizes it as the source artifact for install/deploy.

How to Verify

  1. Save the updated configuration in your pom.xml
  2. Run mvn clean package—check your target directory; you should see both your main JAR and the -sources.jar file
  3. Run mvn clean install—navigate to your local .m2/repository folder, find your project's artifact directory, and confirm both JARs are present there

Troubleshooting If It Still Fails

If the source JAR still isn't generating, run mvn -X package to get verbose logs. Look for errors related to the maven-source-plugin—you'll likely see issues like missing properties or path errors that are causing the plugin to skip execution.

内容的提问来源于stack exchange,提问作者Dmitriy Ryabin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:12:27