diff --git a/src/it/projects/extrajar-missing-file/pom.xml b/src/it/projects/extrajar-missing-file/pom.xml index 56ea5802..58e944e0 100644 --- a/src/it/projects/extrajar-missing-file/pom.xml +++ b/src/it/projects/extrajar-missing-file/pom.xml @@ -46,7 +46,6 @@ under the License. - org.apache.maven.plugins maven-shade-plugin @@ -61,7 +60,7 @@ under the License. true - ${project.build.directory}/dependency/slf4j-simple.jar + ${project.build.directory}/no-such-file.jar diff --git a/src/it/projects/extrajar/pom.xml b/src/it/projects/extrajar/pom.xml index eff4a00b..67b43599 100644 --- a/src/it/projects/extrajar/pom.xml +++ b/src/it/projects/extrajar/pom.xml @@ -46,30 +46,6 @@ under the License. - - org.apache.maven.plugins - maven-dependency-plugin - 3.6.1 - - - copy-extra-jars - package - - copy - - - true - - - org.slf4j - slf4j-simple - ${slf4j.version} - - - - - - org.apache.maven.plugins maven-shade-plugin @@ -83,9 +59,17 @@ under the License. true - - ${project.build.directory}/dependency/slf4j-simple.jar - + + org.slf4j:slf4j-simple:${slf4j.version} + + + + *:* + + META-INF/MANIFEST.MF + + + diff --git a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java index 497ba4a3..93958aa6 100644 --- a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java +++ b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java @@ -77,7 +77,6 @@ import org.eclipse.aether.graph.DependencyNode; import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResolutionException; -import org.eclipse.aether.resolution.ArtifactResult; import static org.apache.maven.plugins.shade.resource.UseDependencyReducedPom.createPomReplaceTransformers; @@ -394,15 +393,35 @@ public class ShadeMojo extends AbstractMojo { private boolean skip; /** - * Extra JAR files to process into shaded result. One can add here "extra JARs" to be processed into the resulting - * shaded JAR. The listed JAR files must exist. Extra JARs will be processed in same way as any other dependency - * (regarding relocation, filtering, resource transformers etc.). + * Extra JAR files to infuse into shaded result. + *

+ * One can add here "extra JARs", to be worked into the resulting shaded JAR. The listed JAR files must exist. + * Extra JARs will be processed in same way as main JAR (if any): applied relocation, resource transformers + * but not filtering. + *

+ * Note: this feature should be used lightly, is not meant as ability to replace dependency hull! It is more + * just a feature to be able to slightly "differentiate" shaded JAR from main only. * * @since 3.6.0 */ @Parameter private List extraJars; + /** + * Extra Artifacts to infuse into shaded result. + *

+ * One can add here "extra Artifacts" to be worked into the resulting shaded JAR. The artifacts will be resolved + * (not transitively), and will be processed in same way as dependency JARs (if any): regarding relocation, + * resource transformers and filtering. + *

+ * Note: this feature should be used lightly, is not meant as ability to replace dependency hull! It is more + * just a feature to be able to slightly "differentiate" shaded JAR from main only. + * + * @since 3.6.0 + */ + @Parameter + private List extraArtifacts; + @Inject private MavenProjectHelper projectHelper; @@ -701,7 +720,8 @@ private void processArtifactSelectors( Set sourceArtifacts, Set testArtifacts, Set testSourceArtifacts, - ArtifactSelector artifactSelector) { + ArtifactSelector artifactSelector) + throws MojoExecutionException { List excludedArtifacts = new ArrayList<>(); List pomArtifacts = new ArrayList<>(); @@ -709,7 +729,28 @@ private void processArtifactSelectors( List emptyTestArtifacts = new ArrayList<>(); List emptyTestSourceArtifacts = new ArrayList<>(); - for (Artifact artifact : project.getArtifacts()) { + ArrayList processedArtifacts = new ArrayList<>(); + if (extraArtifacts != null && !extraArtifacts.isEmpty()) { + processedArtifacts.addAll(extraArtifacts.stream() + .map(org.eclipse.aether.artifact.DefaultArtifact::new) + .map(RepositoryUtils::toArtifact) + .collect(Collectors.toList())); + + for (Artifact artifact : processedArtifacts) { + try { + org.eclipse.aether.artifact.Artifact resolved = + resolveArtifact(RepositoryUtils.toArtifact(artifact)); + if (resolved.getFile() != null) { + artifact.setFile(resolved.getFile()); + } + } catch (ArtifactResolutionException e) { + throw new MojoExecutionException("Could not resolve artifact " + artifact.getId(), e); + } + } + } + processedArtifacts.addAll(project.getArtifacts()); + + for (Artifact artifact : processedArtifacts) { if (!artifactSelector.isSelected(artifact)) { excludedArtifacts.add(artifact.getId()); @@ -823,7 +864,7 @@ private void copyFiles(File source, File target) throws IOException { } private File resolveArtifactForClassifier(Artifact artifact, String classifier) { - org.eclipse.aether.artifact.Artifact coordinate = RepositoryUtils.toArtifact(new DefaultArtifact( + Artifact toResolve = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersionRange() == null @@ -833,24 +874,26 @@ private File resolveArtifactForClassifier(Artifact artifact, String classifier) artifact.getType(), classifier, artifact.getArtifactHandler(), - artifact.isOptional())); - - ArtifactRequest request = new ArtifactRequest( - coordinate, RepositoryUtils.toRepos(project.getRemoteArtifactRepositories()), "shade"); - - Artifact resolvedArtifact; + artifact.isOptional()); try { - ArtifactResult result = repositorySystem.resolveArtifact(session.getRepositorySession(), request); - resolvedArtifact = RepositoryUtils.toArtifact(result.getArtifact()); + org.eclipse.aether.artifact.Artifact resolved = resolveArtifact(RepositoryUtils.toArtifact(toResolve)); + if (resolved.getFile() != null) { + return resolved.getFile(); + } + return null; } catch (ArtifactResolutionException e) { getLog().warn("Could not get " + classifier + " for " + artifact); return null; } + } - if (resolvedArtifact.isResolved()) { - return resolvedArtifact.getFile(); - } - return null; + private org.eclipse.aether.artifact.Artifact resolveArtifact(org.eclipse.aether.artifact.Artifact artifact) + throws ArtifactResolutionException { + return repositorySystem + .resolveArtifact( + session.getRepositorySession(), + new ArtifactRequest(artifact, project.getRemoteProjectRepositories(), "shade")) + .getArtifact(); } private List getRelocators() {