Skip to content

Commit 3d1dbbc

Browse files
committed
[MSHADE-36] Support for injecting the dependency-reduced-pom.xml into the final shaded jar
1 parent da69608 commit 3d1dbbc

File tree

6 files changed

+307
-5
lines changed

6 files changed

+307
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = clean package
19+
invoker.java.version = 1.8+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project>
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>org.apache.maven.plugins.shade.its</groupId>
27+
<artifactId>shade-parent</artifactId>
28+
<version>1.0</version>
29+
<relativePath>../setup-parent</relativePath>
30+
</parent>
31+
32+
<groupId>org.apache.maven.its.shade.drp</groupId>
33+
<artifactId>test</artifactId>
34+
<version>1.0</version>
35+
<packaging>jar</packaging>
36+
37+
<name>MSHADE-36</name>
38+
<description>
39+
Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the normal pom.xml
40+
</description>
41+
42+
<properties>
43+
<maven.compiler.source>1.4</maven.compiler.source>
44+
<maven.compiler.target>1.4</maven.compiler.target>
45+
</properties>
46+
47+
<dependencies>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>3.8.2</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.codehaus.plexus</groupId>
55+
<artifactId>plexus-utils</artifactId>
56+
<version>1.4.1</version>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>2.0.2</version>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-shade-plugin</artifactId>
70+
<version>@project.version@</version>
71+
<executions>
72+
<execution>
73+
<id>attach-shade</id>
74+
<phase>package</phase>
75+
<goals>
76+
<goal>shade</goal>
77+
</goals>
78+
<configuration>
79+
<minimizeJar>true</minimizeJar>
80+
<createDependencyReducedPom>true</createDependencyReducedPom>
81+
<useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
82+
<relocations>
83+
<relocation>
84+
<pattern>org.codehaus.plexus</pattern>
85+
<shadedPattern>nl.example.shaded.org.codehaus.plexus</shadedPattern>
86+
</relocation>
87+
</relocations>
88+
<artifactSet>
89+
<includes>
90+
<include>junit:junit</include>
91+
<include>org.codehaus.plexus:plexus-utils</include>
92+
</includes>
93+
</artifactSet>
94+
</configuration>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package nl.example;
21+
22+
import org.codehaus.plexus.util.StringUtils;
23+
24+
public class Main
25+
{
26+
public static void main( String[] args ) {
27+
System.out.println( "Hello World!" + ( isEmpty("foo") ? " is empty!" : " -- ") );
28+
}
29+
30+
public static boolean isEmpty( String input ) {
31+
return StringUtils.isEmpty( input );
32+
}
33+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.io.*;
21+
import java.util.jar.*;
22+
import java.util.Arrays;
23+
import org.codehaus.plexus.util.*;
24+
25+
String[] wanted =
26+
{
27+
"nl/example/Main.class",
28+
"junit/swingui/icons/error.gif",
29+
"nl/example/shaded/org/codehaus/plexus/util/StringUtils.class",
30+
};
31+
32+
String[] unwanted =
33+
{
34+
"junit/swingui/TestRunner.class",
35+
"org/codehaus/plexus/util/StringUtils.class",
36+
};
37+
38+
JarFile jarFile = null;
39+
try
40+
{
41+
jarFile = new JarFile ( new File( basedir, "target/test-1.0.jar" ) );
42+
43+
for ( String path:wanted )
44+
{
45+
if ( jarFile.getEntry( path ) == null )
46+
{
47+
throw new IllegalStateException( "wanted path is missing: "+path );
48+
}
49+
}
50+
51+
for ( String path:unwanted )
52+
{
53+
if ( jarFile.getEntry( path ) != null )
54+
{
55+
throw new IllegalStateException ( "unwanted path is present: "+path );
56+
}
57+
}
58+
59+
JarEntry jarEntry = jarFile.getEntry( "META-INF/maven/org.apache.maven.its.shade.drp/test/pom.xml" );
60+
String pomFile = IOUtil.toString( jarFile.getInputStream( jarEntry ), "UTF-8" );
61+
62+
if ( pomFile.contains( "<groupId>org.codehaus.plexus</groupId>" ) )
63+
{
64+
throw new IllegalStateException( "The pom.xml still contains a reference to the org.codehaus.plexus dependency" );
65+
}
66+
67+
}
68+
finally
69+
{
70+
if ( jarFile != null ) {
71+
jarFile.close();
72+
}
73+
}

src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import java.util.Set;
7777

7878
import javax.inject.Inject;
79+
import static org.apache.maven.plugins.shade.resource.UseDependencyReducedPom.createPomXmlReplaceTransformers;
7980

8081
/**
8182
* Mojo that performs shading delegating to the Shader component.
@@ -285,6 +286,14 @@ public class ShadeMojo
285286
@Parameter( defaultValue = "false" )
286287
private boolean generateUniqueDependencyReducedPom;
287288

289+
/**
290+
* Do we put the dependency reduced pom in the jar instead of the jar file provided by the project.
291+
*
292+
* @since 3.2.5
293+
*/
294+
@Parameter( defaultValue = "false" )
295+
private boolean useDependencyReducedPomInJar;
296+
288297
/**
289298
* When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.
290299
*/
@@ -449,6 +458,24 @@ public void execute()
449458

450459
List<ResourceTransformer> resourceTransformers = getResourceTransformers();
451460

461+
if ( createDependencyReducedPom )
462+
{
463+
createDependencyReducedPom( artifactIds );
464+
}
465+
466+
if ( useDependencyReducedPomInJar )
467+
{
468+
if ( !createDependencyReducedPom )
469+
{
470+
throw new MojoExecutionException(
471+
"Cannot use the dependency-reduced-pom.xml if it is not created." );
472+
}
473+
474+
// In some cases the used implementation of the resourceTransformers is immutable.
475+
resourceTransformers = new ArrayList<>( resourceTransformers );
476+
resourceTransformers.addAll( createPomXmlReplaceTransformers( project, dependencyReducedPomLocation ) );
477+
}
478+
452479
ShadeRequest shadeRequest = shadeRequest( "jar", artifacts, outputJar, filters, relocators,
453480
resourceTransformers );
454481

@@ -588,11 +615,6 @@ else if ( !renamed )
588615
}
589616
}
590617
}
591-
592-
if ( createDependencyReducedPom )
593-
{
594-
createDependencyReducedPom( artifactIds );
595-
}
596618
}
597619
}
598620
catch ( Exception e )
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.apache.maven.plugins.shade.resource;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.maven.project.MavenProject;
23+
24+
import java.io.File;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
/**
29+
* Manually creates the resource processors needed to remove the original pom.xml and inject
30+
* the dependency-reduced-pom.xml in it's place in the shaded JAR.
31+
*/
32+
public class UseDependencyReducedPom
33+
{
34+
public static List<ResourceTransformer> createPomXmlReplaceTransformers(
35+
MavenProject project,
36+
File dependencyReducedPomLocation
37+
)
38+
{
39+
String pomXmlInFinalJarFilename =
40+
"META-INF/maven/" + project.getGroupId() + "/" + project.getArtifactId() + "/pom.xml";
41+
42+
List<ResourceTransformer> resourceTransformers = new ArrayList<>();
43+
44+
DontIncludeResourceTransformer removePomXML = new DontIncludeResourceTransformer();
45+
removePomXML.resource = pomXmlInFinalJarFilename;
46+
resourceTransformers.add( removePomXML );
47+
48+
IncludeResourceTransformer insertDependencyReducedPomXML = new IncludeResourceTransformer();
49+
insertDependencyReducedPomXML.file = dependencyReducedPomLocation;
50+
insertDependencyReducedPomXML.resource = pomXmlInFinalJarFilename;
51+
resourceTransformers.add( insertDependencyReducedPomXML );
52+
53+
return resourceTransformers;
54+
}
55+
}

0 commit comments

Comments
 (0)