Skip to content

Commit

Permalink
šŸµšŸ¶šŸ»
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsuter committed Jul 19, 2024
1 parent 550ce74 commit 9848bfe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import java.io.File;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;

import ch.ivyteam.ivy.maven.AbstractEngineMojo;
import ch.ivyteam.ivy.maven.engine.EngineClassLoaderFactory;
Expand Down Expand Up @@ -53,8 +53,8 @@ public abstract class AbstractEngineInstanceMojo extends AbstractEngineMojo {
@Component
private RepositorySystem repository;

@Parameter(defaultValue = "${localRepository}")
public ArtifactRepository localRepository;
@Parameter( defaultValue = "${repositorySystemSession}", readonly = true, required = true )
protected RepositorySystemSession repoSession;

private static MavenProjectBuilderProxy builder;

Expand Down Expand Up @@ -97,8 +97,7 @@ protected MavenProjectBuilderProxy getMavenProjectBuilder() throws Exception {
}

public final EngineClassLoaderFactory getEngineClassloaderFactory() {
MavenContext context = new EngineClassLoaderFactory.MavenContext(
repository, localRepository, project, getLog());
MavenContext context = new EngineClassLoaderFactory.MavenContext(repository, project, getLog());
return new EngineClassLoaderFactory(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
Expand Down Expand Up @@ -141,25 +141,27 @@ private static URL[] toUrls(List<File> ivyEngineClassPathFiles) throws Malformed

public static class MavenContext {
private final RepositorySystem repoSystem;
private final ArtifactRepository localRepository;
private final MavenProject project;
private final Log log;

public MavenContext(RepositorySystem repoSystem, ArtifactRepository localRepository, MavenProject project,
public MavenContext(RepositorySystem repoSystem, MavenProject project,
Log log) {
this.repoSystem = repoSystem;
this.localRepository = localRepository;
this.project = project;
this.log = log;
}

public File getJar(String groupId, String artifactId, String version) {
Artifact artifact = repoSystem.createArtifact(groupId, artifactId, version, "jar");
File jar = new File(localRepository.getBasedir(), localRepository.pathOf(artifact));
if (!jar.exists()) {
log.warn("Failed to resolve '" + artifactId + "' from local repository in '" + jar + "'.");
var request = new ArtifactResolutionRequest();
request.setArtifact(artifact);
var response = repoSystem.resolve(request);
if (response.isSuccess()) {
return response.getArtifacts().iterator().next().getFile();
} else {
log.warn("Failed to resolve '" + artifactId + "' from local repository.");
}
return jar;
return null;
}
}

Expand Down
36 changes: 1 addition & 35 deletions src/test/java/ch/ivyteam/ivy/maven/compile/CompileMojoRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

package ch.ivyteam.ivy.maven.compile;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.DefaultArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.internal.DefaultLegacySupport;

import ch.ivyteam.ivy.maven.BaseEngineProjectMojoTest;
import ch.ivyteam.ivy.maven.BaseEngineProjectMojoTest.EngineMojoRule;

public class CompileMojoRule<T extends AbstractEngineInstanceMojo> extends EngineMojoRule<T> {
Expand All @@ -36,30 +28,4 @@ protected void before() throws Throwable {
super.before();
configureMojo(getMojo());
}

public void configureMojo(AbstractEngineInstanceMojo newMojo) throws IllegalAccessException {
newMojo.localRepository = provideLocalRepository();
}

/**
* maven-plugin-testing-harness can not inject local repositories (though the
* real runtime supports it). and the default stubs have no sufficient
* implementation of getPath():
* @see "http://maven.apache.org/plugin-testing/maven-plugin-testing-harness/examples/repositories.html"
*/
private ArtifactRepository provideLocalRepository() throws IllegalAccessException {
DefaultArtifactRepositoryFactory factory = new DefaultArtifactRepositoryFactory();
setVariableValueToObject(factory, "factory",
new org.apache.maven.repository.legacy.repository.DefaultArtifactRepositoryFactory());

LegacySupport legacySupport = new DefaultLegacySupport();
setVariableValueToObject(factory, "legacySupport", legacySupport);

ArtifactRepository localRepository = factory.createArtifactRepository("local", "http://localhost",
new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy());

setVariableValueToObject(localRepository, "basedir", BaseEngineProjectMojoTest.LOCAL_REPOSITORY);

return localRepository;
}
}
}

0 comments on commit 9848bfe

Please sign in to comment.