Skip to content

Commit

Permalink
Make it feature complete
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed May 28, 2024
1 parent 6f03602 commit 3a4c1da
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/it/projects/extrajar-missing-file/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
38 changes: 11 additions & 27 deletions src/it/projects/extrajar/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
81 changes: 62 additions & 19 deletions src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -701,15 +720,37 @@ private void processArtifactSelectors(
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<>();
List<String> emptySourceArtifacts = new ArrayList<>();
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());

Expand Down Expand Up @@ -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
Expand All @@ -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<Relocator> getRelocators() {
Expand Down

0 comments on commit 3a4c1da

Please sign in to comment.