This is an automated email from the ASF dual-hosted git repository. struberg pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git
The following commit(s) were added to refs/heads/master by this push: new 8979c8d - Shade test sources JAR 8979c8d is described below commit 8979c8de64a75e956b058be4d3ce6f1293127b4a Author: Peter De Maeyer <peter.de.mae...@gmail.com> AuthorDate: Sat Nov 9 16:16:16 2019 +0100 - Shade test sources JAR Signed-off-by: Mark Struberg <strub...@apache.org> --- src/it/MSHADE-285_createTestSourcesJar/api/pom.xml | 32 ++++++ .../api/src/main/java/Api.java | 25 +++++ .../api/src/test/java/ApiTest.java | 28 +++++ .../MSHADE-285_createTestSourcesJar/impl/pom.xml | 43 ++++++++ .../impl/src/main/java/Impl.java | 25 +++++ .../impl/src/test/java/ImplTest.java | 28 +++++ src/it/MSHADE-285_createTestSourcesJar/pom.xml | 87 +++++++++++++++ .../MSHADE-285_createTestSourcesJar/uber/pom.xml | 70 ++++++++++++ .../MSHADE-285_createTestSourcesJar/verify.groovy | 31 ++++++ .../apache/maven/plugins/shade/mojo/ShadeMojo.java | 119 ++++++++++++++++----- 10 files changed, 464 insertions(+), 24 deletions(-) diff --git a/src/it/MSHADE-285_createTestSourcesJar/api/pom.xml b/src/it/MSHADE-285_createTestSourcesJar/api/pom.xml new file mode 100644 index 0000000..7d29b5f --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/api/pom.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-parent</artifactId> + <version>1.0</version> + </parent> + <artifactId>mshade-285-api</artifactId> +</project> diff --git a/src/it/MSHADE-285_createTestSourcesJar/api/src/main/java/Api.java b/src/it/MSHADE-285_createTestSourcesJar/api/src/main/java/Api.java new file mode 100644 index 0000000..5900a13 --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/api/src/main/java/Api.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Production API class. + */ +public class Api +{ +} diff --git a/src/it/MSHADE-285_createTestSourcesJar/api/src/test/java/ApiTest.java b/src/it/MSHADE-285_createTestSourcesJar/api/src/test/java/ApiTest.java new file mode 100644 index 0000000..53b3902 --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/api/src/test/java/ApiTest.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Test API class. + */ +public class ApiTest +{ + public static void main(String[] args) { + new Api(); + } +} diff --git a/src/it/MSHADE-285_createTestSourcesJar/impl/pom.xml b/src/it/MSHADE-285_createTestSourcesJar/impl/pom.xml new file mode 100644 index 0000000..e94ed24 --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/impl/pom.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-parent</artifactId> + <version>1.0</version> + </parent> + <artifactId>mshade-285-impl</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-api</artifactId> + <type>test-jar</type> + </dependency> + </dependencies> +</project> diff --git a/src/it/MSHADE-285_createTestSourcesJar/impl/src/main/java/Impl.java b/src/it/MSHADE-285_createTestSourcesJar/impl/src/main/java/Impl.java new file mode 100644 index 0000000..a2e588b --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/impl/src/main/java/Impl.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Production implementation class. + */ +public class Impl extends Api +{ +} diff --git a/src/it/MSHADE-285_createTestSourcesJar/impl/src/test/java/ImplTest.java b/src/it/MSHADE-285_createTestSourcesJar/impl/src/test/java/ImplTest.java new file mode 100644 index 0000000..bd2bf41 --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/impl/src/test/java/ImplTest.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Test implementation class. + */ +public class ImplTest extends ApiTest +{ + public static void main(String[] args) { + new Impl(); + } +} diff --git a/src/it/MSHADE-285_createTestSourcesJar/pom.xml b/src/it/MSHADE-285_createTestSourcesJar/pom.xml new file mode 100644 index 0000000..2f2ea4c --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/pom.xml @@ -0,0 +1,87 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-parent</artifactId> + <packaging>pom</packaging> + <version>1.0</version> + <modules> + <module>api</module> + <module>impl</module> + <module>uber</module> + </modules> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-api</artifactId> + <version>1.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-api</artifactId> + <version>1.0</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-impl</artifactId> + <version>1.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-impl</artifactId> + <version>1.0</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + </dependencies> + </dependencyManagement> + <build> + <plugins> + <plugin> + <artifactId>maven-source-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>test-jar</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>test-jar</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/MSHADE-285_createTestSourcesJar/uber/pom.xml b/src/it/MSHADE-285_createTestSourcesJar/uber/pom.xml new file mode 100644 index 0000000..2ea9240 --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/uber/pom.xml @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-parent</artifactId> + <version>1.0</version> + </parent> + <artifactId>mshade-285-uber</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-api</artifactId> + <type>test-jar</type> + </dependency> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-impl</artifactId> + </dependency> + <dependency> + <groupId>org.apache.maven.its.shade.stj</groupId> + <artifactId>mshade-285-impl</artifactId> + <type>test-jar</type> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <artifactId>maven-shade-plugin</artifactId> + <version>@project.version@</version> + <executions> + <execution> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <createTestSourcesJar>true</createTestSourcesJar> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/MSHADE-285_createTestSourcesJar/verify.groovy b/src/it/MSHADE-285_createTestSourcesJar/verify.groovy new file mode 100644 index 0000000..c72cb75 --- /dev/null +++ b/src/it/MSHADE-285_createTestSourcesJar/verify.groovy @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +def testSourceJarFile = new java.util.jar.JarFile( new File( basedir, "uber/target/mshade-285-uber-1.0-test-sources.jar" ) ) +try +{ + assert null == testSourceJarFile.getJarEntry( "Api.java" ) + assert null == testSourceJarFile.getJarEntry( "Impl.java" ) + assert null != testSourceJarFile.getJarEntry( "ApiTest.java" ) + assert null != testSourceJarFile.getJarEntry( "ImplTest.java" ) +} +finally +{ + testSourceJarFile.close() +} 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 0dd7668..03e70b8 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 @@ -315,6 +315,12 @@ public class ShadeMojo private boolean createSourcesJar; /** + * When true, it will attempt to create a test sources jar. + */ + @Parameter + private boolean createTestSourcesJar; + + /** * When true, it will attempt to shade the contents of the java source files when creating the sources jar. When * false, it will just relocate the java source files to the shaded paths, but will not modify the actual contents * of the java source files. @@ -391,6 +397,7 @@ public class ShadeMojo Set<String> artifactIds = new LinkedHashSet<>(); Set<File> sourceArtifacts = new LinkedHashSet<>(); Set<File> testArtifacts = new LinkedHashSet<>(); + Set<File> testSourceArtifacts = new LinkedHashSet<>(); ArtifactSelector artifactSelector = new ArtifactSelector( project.getArtifact(), artifactSet, shadedGroupFilter ); @@ -423,13 +430,24 @@ public class ShadeMojo testArtifacts.add( file ); } } + + if ( createTestSourcesJar ) + { + File file = shadedTestSourcesArtifactFile(); + if ( file.isFile() ) + { + testSourceArtifacts.add( file ); + } + } } - processArtifactSelectors( artifacts, artifactIds, sourceArtifacts, testArtifacts, artifactSelector ); + processArtifactSelectors( artifacts, artifactIds, sourceArtifacts, testArtifacts, testSourceArtifacts, + artifactSelector ); File outputJar = ( outputFile != null ) ? outputFile : shadedArtifactFileWithClassifier(); File sourcesJar = shadedSourceArtifactFileWithClassifier(); File testJar = shadedTestArtifactFileWithClassifier(); + File testSourcesJar = shadedTestSourceArtifactFileWithClassifier(); // Now add our extra resources try @@ -460,6 +478,15 @@ public class ShadeMojo shader.shade( shadeTestRequest ); } + if ( createTestSourcesJar ) + { + ShadeRequest shadeTestSourcesRequest = + createShadeSourcesRequest( testSourceArtifacts, testSourcesJar, filters, relocators, + resourceTransformers ); + + shader.shade( shadeTestSourcesRequest ); + } + if ( outputFile == null ) { boolean renamed = false; @@ -492,6 +519,14 @@ public class ShadeMojo replaceFile( finalFile, testJar ); testJar = finalFile; } + + if ( createTestSourcesJar ) + { + finalFileName = finalName + "-test-sources.jar"; + finalFile = new File( outputDirectory, finalFileName ); + replaceFile( finalFile, testSourcesJar ); + testSourcesJar = finalFile; + } renamed = true; } @@ -506,6 +541,12 @@ public class ShadeMojo projectHelper.attachArtifact( project, "java-source", shadedClassifierName + "-sources", sourcesJar ); } + + if ( createTestSourcesJar ) + { + projectHelper.attachArtifact( project, "java-source", + shadedClassifierName + "-test-sources", testSourcesJar ); + } } else if ( !renamed ) { @@ -535,6 +576,18 @@ public class ShadeMojo projectHelper.attachArtifact( project, "jar", "tests", shadedTests ); } + if ( createTestSourcesJar ) + { + getLog().info( "Replacing original test source artifact " + + "with shaded test source artifact." ); + File shadedTestSources = shadedTestSourcesArtifactFile(); + + replaceFile( shadedTestSources, testSourcesJar ); + + projectHelper.attachArtifact( project, "java-source", "test-sources", + shadedTestSources ); + } + if ( createDependencyReducedPom ) { createDependencyReducedPom( artifactIds ); @@ -602,7 +655,8 @@ public class ShadeMojo } private void processArtifactSelectors( Set<File> artifacts, Set<String> artifactIds, Set<File> sourceArtifacts, - Set<File> testArtifacts, ArtifactSelector artifactSelector ) + Set<File> testArtifacts, Set<File> testSourceArtifacts, + ArtifactSelector artifactSelector ) { for ( Artifact artifact : project.getArtifacts() ) { @@ -655,6 +709,19 @@ public class ShadeMojo } } } + + if ( createTestSourcesJar ) + { + File file = resolveArtifactForClassifier( artifact, "test-sources" ); + if ( file != null ) + { + testSourceArtifacts.add( file ); + } + else + { + getLog().warn( "Skipping empty test source jar " + artifact.getId() + "." ); + } + } } } @@ -869,40 +936,38 @@ public class ShadeMojo private File shadedSourceArtifactFileWithClassifier() { + return shadedArtifactFileWithClassifier( "sources" ); + } + + private File shadedTestSourceArtifactFileWithClassifier() + { + return shadedArtifactFileWithClassifier( "test-sources" ); + } + + private File shadedArtifactFileWithClassifier( String classifier ) + { Artifact artifact = project.getArtifact(); final String shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName - + "-sources." + artifact.getArtifactHandler().getExtension(); + + "-" + classifier + "." + artifact.getArtifactHandler().getExtension(); return new File( outputDirectory, shadedName ); } private File shadedTestArtifactFileWithClassifier() { - Artifact artifact = project.getArtifact(); - final String shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-" + shadedClassifierName - + "-tests." + artifact.getArtifactHandler().getExtension(); - return new File( outputDirectory, shadedName ); + return shadedArtifactFileWithClassifier( "tests" ); } private File shadedSourcesArtifactFile() { - Artifact artifact = project.getArtifact(); - - String shadedName; - - if ( project.getBuild().getFinalName() != null ) - { - shadedName = project.getBuild().getFinalName() + "-sources." + artifact.getArtifactHandler().getExtension(); - } - else - { - shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-sources." - + artifact.getArtifactHandler().getExtension(); - } + return shadedArtifactFile( "sources" ); + } - return new File( outputDirectory, shadedName ); + private File shadedTestSourcesArtifactFile() + { + return shadedArtifactFile( "test-sources" ); } - private File shadedTestArtifactFile() + private File shadedArtifactFile( String classifier ) { Artifact artifact = project.getArtifact(); @@ -910,17 +975,23 @@ public class ShadeMojo if ( project.getBuild().getFinalName() != null ) { - shadedName = project.getBuild().getFinalName() + "-tests." + artifact.getArtifactHandler().getExtension(); + shadedName = project.getBuild().getFinalName() + "-" + classifier + "." + + artifact.getArtifactHandler().getExtension(); } else { - shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-tests." + shadedName = shadedArtifactId + "-" + artifact.getVersion() + "-" + classifier + "." + artifact.getArtifactHandler().getExtension(); } return new File( outputDirectory, shadedName ); } + private File shadedTestArtifactFile() + { + return shadedArtifactFile( "tests" ); + } + // We need to find the direct dependencies that have been included in the uber JAR so that we can modify the // POM accordingly. private void createDependencyReducedPom( Set<String> artifactsToRemove )