镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public class AggregatorSourceJarMojo extends SourceJarMojo {
*/
@Override
protected void doExecute() throws MojoException {
if (Type.POM.equals(getProject().getPackaging().type().id())) {
packageSources(reactorProjects);
String type = getProject().getPackaging().type().id();
if (!Type.POM.equals(type)) {
throw new MojoException("The aggregate goal requires a project with [" + Type.POM
+ "] packaging. Current project [" + getProject().getId() + "] has a [" + type + "] packaging.");
}
packageSources(reactorProjects);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.
*/
package org.apache.maven.plugins.source;

import java.util.Collections;
import java.util.List;

import org.apache.maven.api.Packaging;
import org.apache.maven.api.Project;
import org.apache.maven.api.Type;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

class AggregatorSourceJarMojoTest {

/**
* Records the projects handed to {@code packageSources} instead of building an archive, so the test can tell
* whether the goal did its work or skipped.
*/
private static class RecordingAggregatorSourceJarMojo extends AggregatorSourceJarMojo {
private List<Project> packagedProjects;

@Override
protected void packageSources(List<Project> theProjects) {
packagedProjects = theProjects;
}
}

@Test
void testPomPackagingPackagesTheReactorProjects() {
Log log = mock(Log.class);
RecordingAggregatorSourceJarMojo mojo = mojoWithPackaging(Type.POM, log);
List<Project> reactorProjects = Collections.singletonList(mock(Project.class));
mojo.reactorProjects = reactorProjects;

mojo.doExecute();

assertEquals(reactorProjects, mojo.packagedProjects);
verifyNoInteractions(log);
}

/**
* The goal cannot aggregate anything for non-POM packaging, so it fails rather than leaving the user with no
* source archive and no reason why.
*/
@Test
void testNonPomPackagingFails() {
RecordingAggregatorSourceJarMojo mojo = mojoWithPackaging("jar", mock(Log.class));

MojoException e = assertThrows(MojoException.class, mojo::doExecute);

assertNull(mojo.packagedProjects, "Sources should not be packaged for non-POM packaging");
assertTrue(e.getMessage().contains("jar"), "Message should name the actual packaging: " + e.getMessage());
assertTrue(e.getMessage().contains("aggregate:1.0:pom"), "Message should name the project: " + e.getMessage());
}

private static RecordingAggregatorSourceJarMojo mojoWithPackaging(String type, Log log) {
Type packagingType = mock(Type.class);
when(packagingType.id()).thenReturn(type);

Packaging packaging = mock(Packaging.class);
when(packaging.type()).thenReturn(packagingType);

Project project = mock(Project.class);
when(project.getPackaging()).thenReturn(packaging);
when(project.getId()).thenReturn("aggregate:1.0:pom");

RecordingAggregatorSourceJarMojo mojo = new RecordingAggregatorSourceJarMojo();
mojo.project = project;
mojo.log = log;
return mojo;
}
}
Loading