Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Feb 6, 2024
1 parent 4622500 commit e73a1c4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import io.takari.incrementalbuild.Incremental;
import io.takari.incrementalbuild.Incremental.Configuration;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -34,17 +32,12 @@
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.artifact.ArtifactType;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.artifact.DefaultArtifactType;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.graph.DefaultDependencyNode;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.resolution.DependencyResult;
import org.eclipse.aether.util.filter.ScopeDependencyFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -87,9 +80,6 @@ public abstract class BaseMojo
@Parameter(required = true, defaultValue = "${basedir}/src/main/provisio")
protected File descriptorDirectory;

@Parameter(defaultValue = "${session}")
protected MavenSession session;

protected ProvisioArtifact projectArtifact() {
ProvisioArtifact jarArtifact = null;
//
Expand Down Expand Up @@ -134,10 +124,10 @@ protected ArtifactSet getRuntimeClasspathAsArtifactSet() {
* <a href="https://issues.apache.org/jira/browse/MNG-8041">MNG-8041</a>
*/
private List<Artifact> resolveRuntimeScopeTransitively() {
DependencyFilter scoopeDependencyFilter = new ScopeDependencyFilter("provided", "test");
DependencyFilter runtimeFilter = new ScopeDependencyFilter("system", "provided", "test");
List<org.eclipse.aether.graph.Dependency> dependencies = project.getDependencies().stream()
.map(d -> RepositoryUtils.toDependency(d, repositorySystemSession.getArtifactTypeRegistry()))
.filter(d -> scoopeDependencyFilter.accept(new DefaultDependencyNode(d), Collections.emptyList()))
.filter(d -> runtimeFilter.accept(new DefaultDependencyNode(d), Collections.emptyList()))
.collect(Collectors.toList());
List<org.eclipse.aether.graph.Dependency> managedDependencies = Collections.emptyList();
if (project.getDependencyManagement() != null) {
Expand All @@ -147,46 +137,21 @@ private List<Artifact> resolveRuntimeScopeTransitively() {
}

CollectRequest collectRequest = new CollectRequest();
collectRequest.setManagedDependencies(managedDependencies);
collectRequest.setRootArtifact(RepositoryUtils.toArtifact(project.getArtifact()));
collectRequest.setRepositories(project.getRemoteProjectRepositories());
collectRequest.setDependencies(dependencies);
collectRequest.setRootArtifact(RepositoryUtils.toArtifact(project.getArtifact()));
DependencyRequest request = new DependencyRequest(collectRequest, scoopeDependencyFilter);
collectRequest.setManagedDependencies(managedDependencies);
DependencyRequest request = new DependencyRequest(collectRequest, runtimeFilter);
try {
DependencyResult dependencyResult = repositorySystem.resolveDependencies(repositorySystemSession, request);
return dependencyResult.getArtifactResults().stream().map(ArtifactResult::getArtifact).collect(Collectors.toList());
return repositorySystem.resolveDependencies(repositorySystemSession, request).getArtifactResults().stream()
.map(ArtifactResult::getArtifact)
.collect(Collectors.toList());
} catch (DependencyResolutionException e) {
logger.error("Failed to resolve runtime dependencies", e);
throw new RuntimeException(e);
}
}

private static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
if (artifact == null) {
return null;
}

String version = artifact.getVersion();
if (version == null && artifact.getVersionRange() != null) {
version = artifact.getVersionRange().toString();
}

Map<String, String> props = null;
if (org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
String localPath = (artifact.getFile() != null) ? artifact.getFile().getPath() : "";
props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, localPath);
}

Artifact result = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), artifact.getArtifactHandler().getExtension(), version, props,
newArtifactType(artifact.getType(), artifact.getArtifactHandler()));
result = result.setFile(artifact.getFile());

return result;
}

private static ArtifactType newArtifactType(String id, ArtifactHandler handler) {
return new DefaultArtifactType(id, handler.getExtension(), handler.getClassifier(), handler.getLanguage(), handler.isAddedToClasspath(), handler.isIncludesDependencies());
}

protected ProvisioningRequest getRequest(Runtime runtime)
{
ProvisioningRequest request = new ProvisioningRequest();
Expand All @@ -212,7 +177,7 @@ protected void checkDuplicates(List<ProvisioArtifact> artifacts)
.filter(strings -> strings.size() > 1)
.map(strings -> String.join(", ", strings))
.collect(Collectors.toList());
if (duplicates.size() != 0) {
if (!duplicates.isEmpty()) {
throw new MojoFailureException("Found different versions of the same dependency: " + String.join(", ", duplicates));
}
}
Expand All @@ -225,10 +190,10 @@ protected List<Dependency> getDependencies(List<ProvisioArtifact> artifacts)
dependency.setGroupId(artifact.getGroupId());
dependency.setArtifactId(artifact.getArtifactId());
dependency.setVersion(artifact.getVersion());
if (artifact.getClassifier() != null && artifact.getClassifier().length() != 0) {
if (artifact.getClassifier() != null && !artifact.getClassifier().isEmpty()) {
dependency.setClassifier(artifact.getClassifier());
}
if (artifact.getExtension() != null && artifact.getExtension().length() != 0 && !artifact.getExtension().equals("jar")) {
if (artifact.getExtension() != null && !artifact.getExtension().isEmpty() && !artifact.getExtension().equals("jar")) {
dependency.setType(artifact.getExtension());
}
dependencies.add(dependency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.WriterFactory;

import java.io.File;
Expand All @@ -48,6 +47,7 @@ public class GeneratorMojo
@Parameter(property = "dependencyExtendedPomLocation", defaultValue = "${project.build.directory}/generated/provisio/dependency-extended-pom.xml")
private File dependencyExtendedPomLocation;

@Override
public void execute()
throws MojoExecutionException, MojoFailureException
{
Expand All @@ -65,7 +65,7 @@ public void execute()
throw new MojoExecutionException("Error resolving artifacts.", e);
}
}
if (artifacts.size() == 0) {
if (artifacts.isEmpty()) {
return;
}
checkDuplicates(artifacts);
Expand All @@ -85,17 +85,11 @@ private void writeModel(Model model)
} catch (IOException e) {
throw new MojoExecutionException("Error creating parent directories for the POM file: " + e.getMessage(), e);
}
Writer writer = null;
try {
writer = WriterFactory.newXmlWriter(dependencyExtendedPomLocation);
try (Writer writer = WriterFactory.newXmlWriter(dependencyExtendedPomLocation)) {
new MavenXpp3Writer().write(writer, model);
writer.close();
}
catch (IOException e) {
throw new MojoExecutionException("Error writing POM file: " + e.getMessage(), e);
}
finally {
IOUtil.close(writer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,24 @@ public List<String> getManagedDependencies(MavenProject project) {
}

public String toCoordinate(Dependency d) {
StringBuffer sb = new StringBuffer().append(d.getGroupId()).append(":").append(d.getArtifactId()).append(":").append(d.getType());
if (d.getClassifier() != null && d.getClassifier().isEmpty() == false) {
StringBuilder sb = new StringBuilder().append(d.getGroupId()).append(":").append(d.getArtifactId()).append(":").append(d.getType());
if (d.getClassifier() != null && !d.getClassifier().isEmpty()) {
sb.append(":").append(d.getClassifier());
}
sb.append(":").append(d.getVersion());
return sb.toString();
}

public String toVersionlessCoordinate(Dependency d) {
StringBuffer sb = new StringBuffer().append(d.getGroupId()).append(":").append(d.getArtifactId()).append(":").append(d.getType());
if (d.getClassifier() != null && d.getClassifier().isEmpty() == false) {
StringBuilder sb = new StringBuilder().append(d.getGroupId()).append(":").append(d.getArtifactId()).append(":").append(d.getType());
if (d.getClassifier() != null && !d.getClassifier().isEmpty()) {
sb.append(":").append(d.getClassifier());
}
return sb.toString();
}

public String toVersionlessCoordinate(MavenProject project) {
String extension = artifactHandlerManager.getArtifactHandler(project.getPackaging()).getExtension();
StringBuffer sb = new StringBuffer().append(project.getGroupId()).append(":").append(project.getArtifactId()).append(":").append(extension);
return sb.toString();
return project.getGroupId() + ":" + project.getArtifactId() + ":" + extension;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import ca.vanzyl.provisio.model.ProvisioningResult;
import ca.vanzyl.provisio.model.Runtime;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -41,6 +40,7 @@ public class ProvisioningMojo extends BaseMojo {
@Parameter(defaultValue = "${project.build.directory}/${project.artifactId}-${project.version}")
private File outputDirectory;

@Override
public void execute() throws MojoExecutionException {
if (skipProvision) {
getLog().info("Skipping provision");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ValidatorMojo
@Parameter(required = true, property = "pomFile", defaultValue = "${basedir}/pom.xml")
private File pomFile;

@Override
public void execute()
throws MojoExecutionException, MojoFailureException
{
Expand Down

0 comments on commit e73a1c4

Please sign in to comment.