Skip to content

Commit 80c9647

Browse files
authored
Fail the aggregate goal for non-POM packaging (#320)
* Warn when aggregate runs on non-POM packaging AggregatorSourceJarMojo.doExecute() packages sources only for pom packaging and otherwise returns without a word, so a project that reaches the goal with some other packaging gets no source archive and no indication of why. Warn instead, naming the project and its actual packaging, in the style of the classifier warning packageSources already emits. * Log the non-POM aggregate skip at info instead of warn aggregate is often declared in a parent POM and inherited by jar modules, where the skip is expected, so warn would flag every one of them. info matches the plugin's other expected-skip messages ("Skipping source per configuration", "NOT adding java-sources ...") and stays visible at the default log level, unlike debug. * Fail the aggregate goal for non-POM packaging There is nothing to aggregate in a project without POM packaging, so stop the build with a message naming the project and its packaging instead of skipping.
1 parent 5f0b0c8 commit 80c9647

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

‎src/main/java/org/apache/maven/plugins/source/AggregatorSourceJarMojo.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ public class AggregatorSourceJarMojo extends SourceJarMojo {
3636
*/
3737
@Override
3838
protected void doExecute() throws MojoException {
39-
if (Type.POM.equals(getProject().getPackaging().type().id())) {
40-
packageSources(reactorProjects);
39+
String type = getProject().getPackaging().type().id();
40+
if (!Type.POM.equals(type)) {
41+
throw new MojoException("The aggregate goal requires a project with [" + Type.POM
42+
+ "] packaging. Current project [" + getProject().getId() + "] has a [" + type + "] packaging.");
4143
}
44+
packageSources(reactorProjects);
4245
}
4346
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
package org.apache.maven.plugins.source;
20+
21+
import java.util.Collections;
22+
import java.util.List;
23+
24+
import org.apache.maven.api.Packaging;
25+
import org.apache.maven.api.Project;
26+
import org.apache.maven.api.Type;
27+
import org.apache.maven.api.plugin.Log;
28+
import org.apache.maven.api.plugin.MojoException;
29+
import org.junit.jupiter.api.Test;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNull;
33+
import static org.junit.jupiter.api.Assertions.assertThrows;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
import static org.mockito.Mockito.mock;
36+
import static org.mockito.Mockito.verifyNoInteractions;
37+
import static org.mockito.Mockito.when;
38+
39+
class AggregatorSourceJarMojoTest {
40+
41+
/**
42+
* Records the projects handed to {@code packageSources} instead of building an archive, so the test can tell
43+
* whether the goal did its work or skipped.
44+
*/
45+
private static class RecordingAggregatorSourceJarMojo extends AggregatorSourceJarMojo {
46+
private List<Project> packagedProjects;
47+
48+
@Override
49+
protected void packageSources(List<Project> theProjects) {
50+
packagedProjects = theProjects;
51+
}
52+
}
53+
54+
@Test
55+
void testPomPackagingPackagesTheReactorProjects() {
56+
Log log = mock(Log.class);
57+
RecordingAggregatorSourceJarMojo mojo = mojoWithPackaging(Type.POM, log);
58+
List<Project> reactorProjects = Collections.singletonList(mock(Project.class));
59+
mojo.reactorProjects = reactorProjects;
60+
61+
mojo.doExecute();
62+
63+
assertEquals(reactorProjects, mojo.packagedProjects);
64+
verifyNoInteractions(log);
65+
}
66+
67+
/**
68+
* The goal cannot aggregate anything for non-POM packaging, so it fails rather than leaving the user with no
69+
* source archive and no reason why.
70+
*/
71+
@Test
72+
void testNonPomPackagingFails() {
73+
RecordingAggregatorSourceJarMojo mojo = mojoWithPackaging("jar", mock(Log.class));
74+
75+
MojoException e = assertThrows(MojoException.class, mojo::doExecute);
76+
77+
assertNull(mojo.packagedProjects, "Sources should not be packaged for non-POM packaging");
78+
assertTrue(e.getMessage().contains("jar"), "Message should name the actual packaging: " + e.getMessage());
79+
assertTrue(e.getMessage().contains("aggregate:1.0:pom"), "Message should name the project: " + e.getMessage());
80+
}
81+
82+
private static RecordingAggregatorSourceJarMojo mojoWithPackaging(String type, Log log) {
83+
Type packagingType = mock(Type.class);
84+
when(packagingType.id()).thenReturn(type);
85+
86+
Packaging packaging = mock(Packaging.class);
87+
when(packaging.type()).thenReturn(packagingType);
88+
89+
Project project = mock(Project.class);
90+
when(project.getPackaging()).thenReturn(packaging);
91+
when(project.getId()).thenReturn("aggregate:1.0:pom");
92+
93+
RecordingAggregatorSourceJarMojo mojo = new RecordingAggregatorSourceJarMojo();
94+
mojo.project = project;
95+
mojo.log = log;
96+
return mojo;
97+
}
98+
}

0 commit comments

Comments
 (0)