Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8023] New method + javadoc on Project #1387

Merged
merged 20 commits into from
Jan 23, 2024
Merged
63 changes: 63 additions & 0 deletions api/maven-api-core/src/main/java/org/apache/maven/api/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,66 @@
@Experimental
public interface Project {

/**
* Returns the project groupId.
*/
@Nonnull
String getGroupId();

/**
* Returns the project artifactId.
*/
@Nonnull
String getArtifactId();

/**
* Returns the project version.
*/
@Nonnull
String getVersion();

/**
* Returns the project packaging.
* <p>
* Note: unlike in legacy code, logical checks against string representing packaging (returned by this method)
* are NOT recommended (code like {@code "pom".equals(project.getPackaging)} must be avoided). Use method
* {@link #getArtifacts()} to gain access to POM or build artifact.
*
* @see #getArtifacts()
*/
@Nonnull
String getPackaging();

/**
* Returns the project artifact, that is the artifact produced by this project build.
*
* @see org.apache.maven.api.services.ArtifactManager#getPath(Artifact)
*/
@Nonnull
michael-o marked this conversation as resolved.
Show resolved Hide resolved
Artifact getArtifact();

/**
* Returns the project artifacts, that is, the project POM artifact and the artifact produced by this project build.
cstamas marked this conversation as resolved.
Show resolved Hide resolved
* The list may have one or two elements (never less than 1, never more than 2), depending on project packaging.
* <p>
* The list's first element is ALWAYS the project POM artifact. Presence of second element in the list depends
* solely on this project packaging.
michael-o marked this conversation as resolved.
Show resolved Hide resolved
*
* @see #getPackaging()
* @see org.apache.maven.api.services.ArtifactManager#getPath(Artifact)
*/
@Nonnull
List<Artifact> getArtifacts();
michael-o marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the project model.
*/
@Nonnull
Model getModel();

/**
* Shorthand method.
*/
@Nonnull
default Build getBuild() {
Build build = getModel().getBuild();
Expand All @@ -71,17 +113,29 @@ default Build getBuild() {
@Nonnull
Optional<Path> getPomPath();

/**
* Returns the project base directory.
*/
@Nonnull
default Optional<Path> getBasedir() {
return getPomPath().map(Path::getParent);
}

/**
* Returns the project direct dependencies (directly specified or inherited).
*/
@Nonnull
List<DependencyCoordinate> getDependencies();

/**
* Returns the project managed dependencies (directly specified or inherited).
*/
@Nonnull
List<DependencyCoordinate> getManagedDependencies();

/**
* Returns the project ID, usable as key.
*/
@Nonnull
default String getId() {
return getModel().getId();
Expand Down Expand Up @@ -126,12 +180,21 @@ default String getId() {
@Nonnull
Path getRootDirectory();

/**
* Returns project parent project, if any.
*/
@Nonnull
Optional<Project> getParent();

/**
* Returns project remote repositories (directly specified or inherited).
*/
@Nonnull
List<RemoteRepository> getRemoteProjectRepositories();

/**
* Returns project remote plugin repositories (directly specified or inherited).
*/
@Nonnull
List<RemoteRepository> getRemotePluginRepositories();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
@Experimental
public interface ProjectManager extends Service {
/**
* Returns the path to the resolved file in the local repository
* if the artifact has been resolved.
* Returns the path to the built project artifact file, if the project has been built.
*
* @return the path of the resolved artifact
* @return the path of the built project artifact
*/
@Nonnull
Optional<Path> getPath(Project project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.model.DependencyManagement;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.ArtifactManager;
import org.apache.maven.api.services.TypeRegistry;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.eclipse.aether.util.artifact.ArtifactIdUtils;

public class DefaultProject implements Project {

Expand Down Expand Up @@ -71,12 +72,21 @@ public String getVersion() {
@Nonnull
@Override
public Artifact getArtifact() {
org.eclipse.aether.artifact.Artifact resolverArtifact = RepositoryUtils.toArtifact(project.getArtifact());
Artifact artifact = session.getArtifact(resolverArtifact);
Path path =
resolverArtifact.getFile() != null ? resolverArtifact.getFile().toPath() : null;
session.getService(ArtifactManager.class).setPath(artifact, path);
return artifact;
return session.getArtifact(RepositoryUtils.toArtifact(project.getArtifact()));
}

@Nonnull
@Override
public List<Artifact> getArtifacts() {
org.eclipse.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact(new ProjectArtifact(project));
org.eclipse.aether.artifact.Artifact projectArtifact = RepositoryUtils.toArtifact(project.getArtifact());

ArrayList<Artifact> result = new ArrayList<>(2);
result.add(session.getArtifact(pomArtifact));
if (!ArtifactIdUtils.equalsVersionlessId(pomArtifact, projectArtifact)) {
result.add(session.getArtifact(projectArtifact));
}
return result;
}

@Nonnull
Expand Down