Skip to content

Commit 83c123d

Browse files
nielsbasjesmichael-o
authored andcommitted
[MSHADE-36] Add option to include dependency reduced POM instead of original one
This closes #25
1 parent da69608 commit 83c123d

File tree

6 files changed

+297
-5
lines changed

6 files changed

+297
-5
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 original pom.xml
40+
</description>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>junit</groupId>
45+
<artifactId>junit</artifactId>
46+
<version>3.8.2</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.codehaus.plexus</groupId>
50+
<artifactId>plexus-utils</artifactId>
51+
<version>1.4.1</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
<version>2.0.2</version>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-shade-plugin</artifactId>
65+
<version>@project.version@</version>
66+
<executions>
67+
<execution>
68+
<id>attach-shade</id>
69+
<phase>package</phase>
70+
<goals>
71+
<goal>shade</goal>
72+
</goals>
73+
<configuration>
74+
<minimizeJar>true</minimizeJar>
75+
<createDependencyReducedPom>true</createDependencyReducedPom>
76+
<useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
77+
<relocations>
78+
<relocation>
79+
<pattern>org.codehaus.plexus</pattern>
80+
<shadedPattern>com.example.shaded.org.codehaus.plexus</shadedPattern>
81+
</relocation>
82+
</relocations>
83+
<artifactSet>
84+
<includes>
85+
<include>junit:junit</include>
86+
<include>org.codehaus.plexus:plexus-utils</include>
87+
</includes>
88+
</artifactSet>
89+
</configuration>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
</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 com.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+
"com/example/Main.class",
28+
"junit/swingui/icons/error.gif",
29+
"com/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: 23 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.createPomReplaceTransformers;
7980

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

289+
/**
290+
* Add dependency reduced POM to the JAR instead of the original one provided by the project.
291+
* If {@code createDependencyReducedPom} is {@code false} this parameter will be ignored.
292+
*
293+
* @since 3.3.0
294+
*/
295+
@Parameter( defaultValue = "false" )
296+
private boolean useDependencyReducedPomInJar;
297+
288298
/**
289299
* When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.
290300
*/
@@ -449,6 +459,19 @@ public void execute()
449459

450460
List<ResourceTransformer> resourceTransformers = getResourceTransformers();
451461

462+
if ( createDependencyReducedPom )
463+
{
464+
createDependencyReducedPom( artifactIds );
465+
466+
if ( useDependencyReducedPomInJar )
467+
{
468+
// In some cases the used implementation of the resourceTransformers is immutable.
469+
resourceTransformers = new ArrayList<>( resourceTransformers );
470+
resourceTransformers.addAll(
471+
createPomReplaceTransformers( project, dependencyReducedPomLocation ) );
472+
}
473+
}
474+
452475
ShadeRequest shadeRequest = shadeRequest( "jar", artifacts, outputJar, filters, relocators,
453476
resourceTransformers );
454477

@@ -588,11 +611,6 @@ else if ( !renamed )
588611
}
589612
}
590613
}
591-
592-
if ( createDependencyReducedPom )
593-
{
594-
createDependencyReducedPom( artifactIds );
595-
}
596614
}
597615
}
598616
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 its place in the shaded JAR.
31+
*/
32+
public class UseDependencyReducedPom
33+
{
34+
public static List<ResourceTransformer> createPomReplaceTransformers(
35+
MavenProject project,
36+
File dependencyReducedPomLocation
37+
)
38+
{
39+
String pomInFinalJarFilename =
40+
"META-INF/maven/" + project.getGroupId() + "/" + project.getArtifactId() + "/pom.xml";
41+
42+
List<ResourceTransformer> resourceTransformers = new ArrayList<>();
43+
44+
DontIncludeResourceTransformer removePom = new DontIncludeResourceTransformer();
45+
removePom.resource = pomInFinalJarFilename;
46+
resourceTransformers.add( removePom );
47+
48+
IncludeResourceTransformer insertDependencyReducedPom = new IncludeResourceTransformer();
49+
insertDependencyReducedPom.file = dependencyReducedPomLocation;
50+
insertDependencyReducedPom.resource = pomInFinalJarFilename;
51+
resourceTransformers.add( insertDependencyReducedPom );
52+
53+
return resourceTransformers;
54+
}
55+
}

0 commit comments

Comments
 (0)