Skip to content

Commit

Permalink
Removed the dummy remote repository in favour for the snapshot and re… (
Browse files Browse the repository at this point in the history
  • Loading branch information
markrekveld authored Dec 27, 2022
1 parent f13162a commit 4230186
Showing 1 changed file with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jfrog.build.extractor.maven;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
Expand All @@ -9,37 +10,64 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.jfrog.build.extractor.maven.resolver.ResolutionHelper;
import org.sonatype.aether.repository.RepositoryPolicy;

import javax.inject.Named;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import static org.jfrog.build.api.BuildInfoConfigProperties.PROP_ARTIFACTORY_RESOLUTION_ENABLED;

@Named
@Component( role = DefaultProjectBuilder.class, hint = "default")
@Component(role = DefaultProjectBuilder.class, hint = "default")
public class ArtifactoryProjectBuilder extends DefaultProjectBuilder {

@Requirement
private ResolutionHelper resolutionHelper;

@Override
public List<ProjectBuildingResult> build(List<File> pomFiles, boolean recursive, ProjectBuildingRequest request) throws ProjectBuildingException {
if (Boolean.parseBoolean(System.getProperties().getProperty(PROP_ARTIFACTORY_RESOLUTION_ENABLED))
|| Boolean.parseBoolean(System.getenv(PROP_ARTIFACTORY_RESOLUTION_ENABLED))) {
// We're setting a dummy repository to the list of repositories, which has both snapshot and release policies enabled.
if (!resolutionHelper.isInitialized()) {
Properties allMavenProps = new Properties();
allMavenProps.putAll(request.getSystemProperties());
allMavenProps.putAll(request.getUserProperties());
resolutionHelper.init(allMavenProps);
}

// We're setting the resolver repositories to the list of repositories.
// This repository replaces the central repository.
// This ensures that parent poms with snapshot versions can be downloaded from Artifactory.
request.setRemoteRepositories(getDummyRepo());
List<ArtifactRepository> repositories = getRepositories();
request.setRemoteRepositories(repositories);
request.setPluginArtifactRepositories(repositories);
}
return super.build(pomFiles, recursive, request);
}

private List<ArtifactRepository> getDummyRepo() {
ArtifactRepository repo = new MavenArtifactRepository("dummy",
"http://ArtifactoryDummyRepo./dummy",
new DefaultRepositoryLayout(),
new ArtifactRepositoryPolicy(),
new ArtifactRepositoryPolicy());
return new ArrayList<ArtifactRepository>(Collections.singletonList(repo));
private List<ArtifactRepository> getRepositories() {
String releaseRepoUrl = resolutionHelper.getRepoReleaseUrl();
String snapshotRepoUrl = resolutionHelper.getRepoSnapshotUrl();

List<ArtifactRepository> repositories = new ArrayList<>();
if (StringUtils.isNotBlank(snapshotRepoUrl)) {
ArtifactRepositoryPolicy snapshotPolicy = new ArtifactRepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
ArtifactRepositoryPolicy releasePolicy = new ArtifactRepositoryPolicy(false, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
repositories.add(new MavenArtifactRepository("artifactory-snapshot", snapshotRepoUrl, new DefaultRepositoryLayout(), snapshotPolicy, releasePolicy));
}
if (StringUtils.isNotBlank(releaseRepoUrl)) {
boolean snapshotPolicyEnabled = StringUtils.isBlank(snapshotRepoUrl);
String repositoryId = snapshotPolicyEnabled ? "artifactory-release-snapshot" : "artifactory-release";

ArtifactRepositoryPolicy snapshotPolicy = new ArtifactRepositoryPolicy(snapshotPolicyEnabled, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
ArtifactRepositoryPolicy releasePolicy = new ArtifactRepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
repositories.add(new MavenArtifactRepository(repositoryId, releaseRepoUrl, new DefaultRepositoryLayout(), snapshotPolicy, releasePolicy));
}
return repositories;
}
}

0 comments on commit 4230186

Please sign in to comment.