This is an automated email from the ASF dual-hosted git repository. cstamas pushed a commit to branch MSHADE-478-p2 in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git
commit 3a4c1dab7e0affd34dc0ebad83bdc032e1d3efa0 Author: Tamas Cservenak <[email protected]> AuthorDate: Tue May 28 09:21:31 2024 +0200 Make it feature complete --- src/it/projects/extrajar-missing-file/pom.xml | 3 +- src/it/projects/extrajar/pom.xml | 38 +++------- .../apache/maven/plugins/shade/mojo/ShadeMojo.java | 81 +++++++++++++++++----- 3 files changed, 74 insertions(+), 48 deletions(-) diff --git a/src/it/projects/extrajar-missing-file/pom.xml b/src/it/projects/extrajar-missing-file/pom.xml index 56ea580..58e944e 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. <build> <plugins> - <!-- See extrajar IT: removed m-dep-p:copy so the file is not there --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> @@ -61,7 +60,7 @@ under the License. <configuration> <shadedArtifactAttached>true</shadedArtifactAttached> <extraJars> - <extraJar>${project.build.directory}/dependency/slf4j-simple.jar</extraJar> + <extraJar>${project.build.directory}/no-such-file.jar</extraJar> </extraJars> </configuration> </execution> diff --git a/src/it/projects/extrajar/pom.xml b/src/it/projects/extrajar/pom.xml index eff4a00..67b4359 100644 --- a/src/it/projects/extrajar/pom.xml +++ b/src/it/projects/extrajar/pom.xml @@ -46,30 +46,6 @@ under the License. <build> <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-dependency-plugin</artifactId> - <version>3.6.1</version> - <executions> - <execution> - <id>copy-extra-jars</id> - <phase>package</phase> - <goals> - <goal>copy</goal> - </goals> - <configuration> - <stripVersion>true</stripVersion> - <artifactItems> - <artifactItem> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-simple</artifactId> - <version>${slf4j.version}</version> - </artifactItem> - </artifactItems> - </configuration> - </execution> - </executions> - </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> @@ -83,9 +59,17 @@ under the License. </goals> <configuration> <shadedArtifactAttached>true</shadedArtifactAttached> - <extraJars> - <extraJar>${project.build.directory}/dependency/slf4j-simple.jar</extraJar> - </extraJars> + <extraArtifacts> + <extraArtifact>org.slf4j:slf4j-simple:${slf4j.version}</extraArtifact> + </extraArtifacts> + <filters> + <filter> + <artifact>*:*</artifact> + <excludes> + <exclude>META-INF/MANIFEST.MF</exclude> + </excludes> + </filter> + </filters> </configuration> </execution> </executions> 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 497ba4a..93958aa 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.collection.DependencyCollectionException; 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. + * <p> + * 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. + * <p> + * 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<File> extraJars; + /** + * Extra Artifacts to infuse into shaded result. + * <p> + * 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. + * <p> + * 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<String> extraArtifacts; + @Inject private MavenProjectHelper projectHelper; @@ -701,7 +720,8 @@ public class ShadeMojo extends AbstractMojo { Set<File> sourceArtifacts, Set<File> testArtifacts, Set<File> testSourceArtifacts, - ArtifactSelector artifactSelector) { + ArtifactSelector artifactSelector) + throws MojoExecutionException { List<String> excludedArtifacts = new ArrayList<>(); List<String> pomArtifacts = new ArrayList<>(); @@ -709,7 +729,28 @@ public class ShadeMojo extends AbstractMojo { List<String> emptyTestArtifacts = new ArrayList<>(); List<String> emptyTestSourceArtifacts = new ArrayList<>(); - for (Artifact artifact : project.getArtifacts()) { + ArrayList<Artifact> 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 @@ public class ShadeMojo extends AbstractMojo { } 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 @@ public class ShadeMojo extends AbstractMojo { 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<Relocator> getRelocators() {
