From a9ce5eded2aa39a096d9a2c836183994b432b4f0 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 4 Aug 2026 14:23:33 +0000 Subject: [PATCH 1/3] Fix TestSourceJarMojo execute phase to generate-test-sources Closes #304 The TestSourceJarMojo was forking to generate-sources instead of generate-test-sources. This caused test sources generated by other plugins in the generate-test-sources phase to not be available when the mojo runs, leading to an empty or incomplete test sources JAR. - Changed @Execute(phase = generate-sources) to @Execute(phase = generate-test-sources) - Added test to verify the correct phase is used --- .../apache/maven/plugins/source/TestSourceJarMojo.java | 2 +- .../maven/plugins/source/TestSourceJarMojoTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/maven/plugins/source/TestSourceJarMojo.java b/src/main/java/org/apache/maven/plugins/source/TestSourceJarMojo.java index dd281dd..1976407 100644 --- a/src/main/java/org/apache/maven/plugins/source/TestSourceJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/source/TestSourceJarMojo.java @@ -27,7 +27,7 @@ * @since 2.0.3 */ @Mojo(name = "test-jar", defaultPhase = "package") -@Execute(phase = "generate-sources") +@Execute(phase = "generate-test-sources") public class TestSourceJarMojo extends TestSourceJarNoForkMojo { // no op } diff --git a/src/test/java/org/apache/maven/plugins/source/TestSourceJarMojoTest.java b/src/test/java/org/apache/maven/plugins/source/TestSourceJarMojoTest.java index e0bc5fe..8ea2f06 100644 --- a/src/test/java/org/apache/maven/plugins/source/TestSourceJarMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/source/TestSourceJarMojoTest.java @@ -25,6 +25,7 @@ import org.apache.maven.api.Project; import org.apache.maven.api.ProjectScope; import org.apache.maven.api.di.Provides; +import org.apache.maven.api.plugin.annotations.Execute; import org.apache.maven.api.plugin.testing.Basedir; import org.apache.maven.api.plugin.testing.InjectMojo; import org.apache.maven.api.plugin.testing.MojoParameter; @@ -35,6 +36,7 @@ import org.junit.jupiter.api.Test; import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -133,6 +135,12 @@ public void testIncludeMavenDescriptorWhenExplicitlyConfigured(AbstractSourceJar }); } + @Test + void testExecutePhase() { + Execute execute = TestSourceJarMojo.class.getAnnotation(Execute.class); + assertEquals("generate-test-sources", execute.phase()); + } + @Provides InternalSession createSession() { InternalSession session = SessionMock.getMockSession("target/local-repo"); From 17cab2856ece72c9f4d2f65ef569291c09143d62 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 4 Aug 2026 15:08:38 +0000 Subject: [PATCH 2/3] Add integration test for test-jar generate-test-sources phase This integration test verifies that TestSourceJarMojo forks to generate-test-sources phase. It uses maven-antrun-plugin to generate a file in generate-test-sources phase and verifies it is included in the test sources JAR. Before the fix, the test-jar goal forks to generate-sources, so the antrun execution in generate-test-sources phase does not run and the generated file is not in the JAR. After the fix, the test-jar goal forks to generate-test-sources, so the antrun execution runs and the generated file is in the JAR. --- .../invoker.properties | 18 ++++++ src/it/test-jar-generate-test-sources/pom.xml | 63 +++++++++++++++++++ .../test-jar-generate-test-sources/verify.bsh | 58 +++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/it/test-jar-generate-test-sources/invoker.properties create mode 100644 src/it/test-jar-generate-test-sources/pom.xml create mode 100644 src/it/test-jar-generate-test-sources/verify.bsh diff --git a/src/it/test-jar-generate-test-sources/invoker.properties b/src/it/test-jar-generate-test-sources/invoker.properties new file mode 100644 index 0000000..1dc843a --- /dev/null +++ b/src/it/test-jar-generate-test-sources/invoker.properties @@ -0,0 +1,18 @@ +# 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. + +invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:test-jar diff --git a/src/it/test-jar-generate-test-sources/pom.xml b/src/it/test-jar-generate-test-sources/pom.xml new file mode 100644 index 0000000..36b45c9 --- /dev/null +++ b/src/it/test-jar-generate-test-sources/pom.xml @@ -0,0 +1,63 @@ + + + + 4.0.0 + + org.apache.maven.its + test-jar-generate-test-sources + 1.0-SNAPSHOT + + Test for test-jar with generated test sources + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-source-plugin + @project.version@ + + + org.apache.maven.plugins + maven-antrun-plugin + 1.3 + + + generate-test-sources + + run + + generate-test-sources + + + + + target/generated-test-sources/ant + + + + + + + + diff --git a/src/it/test-jar-generate-test-sources/verify.bsh b/src/it/test-jar-generate-test-sources/verify.bsh new file mode 100644 index 0000000..243c87b --- /dev/null +++ b/src/it/test-jar-generate-test-sources/verify.bsh @@ -0,0 +1,58 @@ +/* + * 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. + */ + +import java.io.*; +import java.util.*; +import java.util.jar.*; + +try +{ + File jarFile = new File( basedir, "target/test-jar-generate-test-sources-1.0-SNAPSHOT-test-sources.jar" ); + System.out.println( "Checking for existence of " + jarFile ); + if ( !jarFile.isFile() ) + { + System.out.println( "FAILURE! Test sources JAR does not exist." ); + return false; + } + + JarFile jar = new JarFile( jarFile ); + + String[] includedEntries = { + "META-INF/MANIFEST.MF", + "GeneratedTest.java", + }; + for ( String included : includedEntries ) + { + System.out.println( "Checking for existence of " + included ); + if ( jar.getEntry( included ) == null ) + { + System.out.println( "FAILURE! Expected entry not found: " + included ); + return false; + } + } + + System.out.println( "SUCCESS! Generated test sources are included in the test sources JAR." ); +} +catch( Throwable t ) +{ + t.printStackTrace(); + return false; +} + +return true; From 022c8b68408779b2244aa7c98392e8d5c37acf46 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 4 Aug 2026 18:06:40 +0000 Subject: [PATCH 3/3] Update verify.bsh --- src/it/test-jar-generate-test-sources/verify.bsh | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/it/test-jar-generate-test-sources/verify.bsh b/src/it/test-jar-generate-test-sources/verify.bsh index 243c87b..45f27bd 100644 --- a/src/it/test-jar-generate-test-sources/verify.bsh +++ b/src/it/test-jar-generate-test-sources/verify.bsh @@ -24,7 +24,6 @@ import java.util.jar.*; try { File jarFile = new File( basedir, "target/test-jar-generate-test-sources-1.0-SNAPSHOT-test-sources.jar" ); - System.out.println( "Checking for existence of " + jarFile ); if ( !jarFile.isFile() ) { System.out.println( "FAILURE! Test sources JAR does not exist." ); @@ -39,19 +38,16 @@ try }; for ( String included : includedEntries ) { - System.out.println( "Checking for existence of " + included ); if ( jar.getEntry( included ) == null ) { System.out.println( "FAILURE! Expected entry not found: " + included ); return false; } } - - System.out.println( "SUCCESS! Generated test sources are included in the test sources JAR." ); } -catch( Throwable t ) +catch( Exception ex ) { - t.printStackTrace(); + ex.printStackTrace(); return false; }