mirror of https://github.com/apache/maven.git
[MNG-8330] Enforce attached artifacts to have the same GAV than their project (#1838)
This commit is contained in:
parent
d62ecadb35
commit
dc6d48ce00
|
@ -71,7 +71,16 @@ public interface ProjectManager extends Service {
|
||||||
*/
|
*/
|
||||||
Collection<ProducedArtifact> getAllArtifacts(Project project);
|
Collection<ProducedArtifact> getAllArtifacts(Project project);
|
||||||
|
|
||||||
default void attachArtifact(Session session, Project project, Path path) {
|
/**
|
||||||
|
* Attaches an artifact to the project using the given file path. The artifact type will be
|
||||||
|
* determined from the file extension.
|
||||||
|
*
|
||||||
|
* @param session the current build session
|
||||||
|
* @param project the project to attach the artifact to
|
||||||
|
* @param path the path to the artifact file
|
||||||
|
* @throws IllegalArgumentException if the session, project or path is null
|
||||||
|
*/
|
||||||
|
default void attachArtifact(@Nonnull Session session, @Nonnull Project project, @Nonnull Path path) {
|
||||||
String name = path.getFileName().toString();
|
String name = path.getFileName().toString();
|
||||||
int dot = name.lastIndexOf('.');
|
int dot = name.lastIndexOf('.');
|
||||||
String ext = dot >= 1 ? name.substring(dot + 1) : "";
|
String ext = dot >= 1 ? name.substring(dot + 1) : "";
|
||||||
|
@ -80,13 +89,33 @@ public interface ProjectManager extends Service {
|
||||||
attachArtifact(project, artifact, path);
|
attachArtifact(project, artifact, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
default void attachArtifact(Session session, Project project, String type, Path path) {
|
/**
|
||||||
|
* Attaches an artifact to the project with an explicitly specified type.
|
||||||
|
*
|
||||||
|
* @param session the current build session
|
||||||
|
* @param project the project to attach the artifact to
|
||||||
|
* @param type the type of the artifact (e.g., "jar", "war", "sources")
|
||||||
|
* @param path the path to the artifact file
|
||||||
|
* @throws IllegalArgumentException if the session, project, type or path is null
|
||||||
|
* @see org.apache.maven.api.Type
|
||||||
|
*/
|
||||||
|
default void attachArtifact(
|
||||||
|
@Nonnull Session session, @Nonnull Project project, @Nonnull String type, @Nonnull Path path) {
|
||||||
ProducedArtifact artifact = session.createProducedArtifact(
|
ProducedArtifact artifact = session.createProducedArtifact(
|
||||||
project.getGroupId(), project.getArtifactId(), project.getVersion(), null, null, type);
|
project.getGroupId(), project.getArtifactId(), project.getVersion(), null, null, type);
|
||||||
attachArtifact(project, artifact, path);
|
attachArtifact(project, artifact, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void attachArtifact(Project project, ProducedArtifact artifact, Path path);
|
/**
|
||||||
|
* Attaches a produced artifact to the project at the specified path. This is the base method
|
||||||
|
* that the other attachArtifact methods delegate to.
|
||||||
|
*
|
||||||
|
* @param project the project to attach the artifact to
|
||||||
|
* @param artifact the produced artifact to attach
|
||||||
|
* @param path the path to the artifact file
|
||||||
|
* @throws IllegalArgumentException if the project, artifact or path is null
|
||||||
|
*/
|
||||||
|
void attachArtifact(@Nonnull Project project, @Nonnull ProducedArtifact artifact, @Nonnull Path path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtain an immutable list of compile source roots for the given project and scope.
|
* Obtain an immutable list of compile source roots for the given project and scope.
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* 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.internal.impl;
|
||||||
|
|
||||||
|
import org.apache.maven.api.Version;
|
||||||
|
import org.eclipse.aether.util.version.GenericVersionScheme;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class DefaultModelVersionParserTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void parseVersion() {
|
||||||
|
Version v = new DefaultModelVersionParser(new GenericVersionScheme()).parseVersion("");
|
||||||
|
assertNotNull(v);
|
||||||
|
assertEquals("", v.asString());
|
||||||
|
}
|
||||||
|
}
|
|
@ -28,6 +28,7 @@ import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -93,6 +94,32 @@ public class DefaultProjectManager implements ProjectManager {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void attachArtifact(Project project, ProducedArtifact artifact, Path path) {
|
public void attachArtifact(Project project, ProducedArtifact artifact, Path path) {
|
||||||
|
nonNull(project, "project");
|
||||||
|
nonNull(artifact, "artifact");
|
||||||
|
nonNull(path, "path");
|
||||||
|
if (artifact.getGroupId().isEmpty()
|
||||||
|
|| artifact.getArtifactId().isEmpty()
|
||||||
|
|| artifact.getBaseVersion().asString().isEmpty()) {
|
||||||
|
artifact = session.createProducedArtifact(
|
||||||
|
artifact.getGroupId().isEmpty() ? project.getGroupId() : artifact.getGroupId(),
|
||||||
|
artifact.getArtifactId().isEmpty() ? project.getArtifactId() : artifact.getArtifactId(),
|
||||||
|
artifact.getBaseVersion().asString().isEmpty()
|
||||||
|
? session.parseVersion(project.getVersion()).asString()
|
||||||
|
: artifact.getBaseVersion().asString(),
|
||||||
|
artifact.getClassifier(),
|
||||||
|
artifact.getExtension(),
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
if (!Objects.equals(project.getGroupId(), artifact.getGroupId())
|
||||||
|
|| !Objects.equals(project.getArtifactId(), artifact.getArtifactId())
|
||||||
|
|| !Objects.equals(
|
||||||
|
project.getVersion(), artifact.getBaseVersion().asString())) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The produced artifact must have the same groupId/artifactId/version than the project it is attached to. Expecting "
|
||||||
|
+ project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion()
|
||||||
|
+ " but received " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
|
||||||
|
+ artifact.getBaseVersion());
|
||||||
|
}
|
||||||
getMavenProject(project)
|
getMavenProject(project)
|
||||||
.addAttachedArtifact(RepositoryUtils.toArtifact(
|
.addAttachedArtifact(RepositoryUtils.toArtifact(
|
||||||
((DefaultProject) project).getSession().toArtifact(artifact)));
|
((DefaultProject) project).getSession().toArtifact(artifact)));
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
/*
|
||||||
|
* 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.internal.impl;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
import org.apache.maven.api.ProducedArtifact;
|
||||||
|
import org.apache.maven.api.Project;
|
||||||
|
import org.apache.maven.api.services.ArtifactManager;
|
||||||
|
import org.apache.maven.project.MavenProject;
|
||||||
|
import org.eclipse.aether.util.version.GenericVersionScheme;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
class DefaultProjectManagerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void attachArtifact() {
|
||||||
|
InternalMavenSession session = Mockito.mock(InternalMavenSession.class);
|
||||||
|
ArtifactManager artifactManager = Mockito.mock(ArtifactManager.class);
|
||||||
|
MavenProject mavenProject = new MavenProject();
|
||||||
|
Project project = new DefaultProject(session, mavenProject);
|
||||||
|
ProducedArtifact artifact = Mockito.mock(ProducedArtifact.class);
|
||||||
|
Path path = Paths.get("");
|
||||||
|
DefaultVersionParser versionParser =
|
||||||
|
new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme()));
|
||||||
|
DefaultProjectManager projectManager = new DefaultProjectManager(session, artifactManager);
|
||||||
|
|
||||||
|
mavenProject.setGroupId("myGroup");
|
||||||
|
mavenProject.setArtifactId("myArtifact");
|
||||||
|
mavenProject.setVersion("1.0-SNAPSHOT");
|
||||||
|
when(artifact.getGroupId()).thenReturn("myGroup");
|
||||||
|
when(artifact.getArtifactId()).thenReturn("myArtifact");
|
||||||
|
when(artifact.getBaseVersion()).thenReturn(versionParser.parseVersion("1.0-SNAPSHOT"));
|
||||||
|
projectManager.attachArtifact(project, artifact, path);
|
||||||
|
|
||||||
|
when(artifact.getArtifactId()).thenReturn("anotherArtifact");
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> projectManager.attachArtifact(project, artifact, path));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue