Maven源码生成异常及自动打包配置问题求助
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
jargoal instead of the more efficientjar-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
- 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.
jar-no-forkGoal: This skips re-compiling your code, making the build faster and avoiding any compilation-related issues that might break source JAR generation.- Standard Properties: Using
${project.build.directory}(which maps to yourtargetfolder) ensures the plugin always knows where to output the JAR, no extra property setup needed. - Classifier: Using the standard
sourcesclassifier ensures your source JAR is named likemodule-sources.jar—matching what you get when runningsource:jarmanually, and ensuring Maven recognizes it as the source artifact for install/deploy.
How to Verify
- Save the updated configuration in your pom.xml
- Run
mvn clean package—check yourtargetdirectory; you should see both your main JAR and the-sources.jarfile - Run
mvn clean install—navigate to your local.m2/repositoryfolder, 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

