org.apache.maven.artifact.repository.MavenArtifactRepository Java Examples

The following examples show how to use org.apache.maven.artifact.repository.MavenArtifactRepository. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MavenUtilTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private MavenSession getMavenSession() {
    Settings settings = new Settings();
    ArtifactRepository localRepository = new MavenArtifactRepository() {
        public String getBasedir() {
            return "repository";
        }
    };

    Properties userProperties = new Properties();
    userProperties.put("user.maven.home", "/home/user/.m2");

    Properties systemProperties = new Properties();
    systemProperties.put("foo", "bar");

    return new MavenSession(null, settings, localRepository, null, null, Collections.<String>emptyList(), ".", systemProperties, userProperties, new Date(System.currentTimeMillis()));
}
 
Example #2
Source File: AbstractUnleashMojo.java    From unleash-maven-plugin with Eclipse Public License 1.0 6 votes vote down vote up
@MojoProduces
@Named("additionalDeployemntRepositories")
private Set<RemoteRepository> getAdditionalDeploymentRepositories() {
  Set<Repository> repos = new HashSet<>();
  if (this.additionalDeploymentRepositories != null) {
    repos.addAll(this.additionalDeploymentRepositories);
  }

  System.getProperties().forEach((key, value) -> {
    if (key.toString().startsWith(PROPERTY_REPO_BASE)) {
      Repository.parseFromProperty(value.toString()).ifPresent(repo -> repos.add(repo));
    }
  });

  return repos.stream().map(repo -> {
    DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
    ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy();
    ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy();

    ArtifactRepository artifactRepository = new MavenArtifactRepository(repo.getId(), repo.getUrl(), layout,
        snapshotsPolicy, releasesPolicy);
    this.settings.getServers().stream().filter(server -> Objects.equals(server.getId(), repo.getId())).findFirst()
        .ifPresent(server -> artifactRepository.setAuthentication(createServerAuthentication(server)));
    return RepositoryUtils.toRepo(artifactRepository);
  }).collect(Collectors.toSet());
}
 
Example #3
Source File: MavenArtifactResolvingHelperTest.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@Test
// SWARM-1173: swarm-plugin trying to download SNAPSHOTs from Maven Central
public void propagateRepositoryPolicies() {
    MavenArtifactResolvingHelper artifactResolverHelper = new MavenArtifactResolvingHelper(null, null, sessionMock, null);
    ArtifactRepositoryPolicy testSnapshotPolicy = new ArtifactRepositoryPolicy(false, "hourly", "warn");
    ArtifactRepositoryPolicy testReleasesPolicy = new ArtifactRepositoryPolicy(true, "never", "warn");
    ArtifactRepository testingRepo = new MavenArtifactRepository("testing-repo", "http://testing-repo.org", new DefaultRepositoryLayout(), testSnapshotPolicy, testReleasesPolicy);
    artifactResolverHelper.remoteRepository(testingRepo);

    List<RemoteRepository> remoteRepos = artifactResolverHelper.getRemoteRepositories();

    Assert.assertTrue("Remote repositories " + remoteRepos + " do not contain expected testing repo " + testingRepo,
                      remoteRepos.stream().anyMatch(
                              remoteRepo -> {
                                  RepositoryPolicy snapshotsPolicy = remoteRepo.getPolicy(true);
                                  RepositoryPolicy releasesPolicy = remoteRepo.getPolicy(false);
                                  return remoteRepo.getId().equals(testingRepo.getId()) &&
                                          !snapshotsPolicy.isEnabled() &&
                                          snapshotsPolicy.getUpdatePolicy().equals(testSnapshotPolicy.getUpdatePolicy()) &&
                                          snapshotsPolicy.getChecksumPolicy().equals(testSnapshotPolicy.getChecksumPolicy()) &&
                                          releasesPolicy.isEnabled() &&
                                          releasesPolicy.getUpdatePolicy().equals(testReleasesPolicy.getUpdatePolicy()) &&
                                          releasesPolicy.getChecksumPolicy().equals(testReleasesPolicy.getChecksumPolicy());
                              })
    );
}
 
Example #4
Source File: DockerFileUtilTest.java    From docker-maven-plugin with Apache License 2.0 6 votes vote down vote up
private MojoParameters mockMojoParams() {
    MavenProject project = new MavenProject();
    project.setArtifactId("docker-maven-plugin");

    Properties projectProperties = project.getProperties();
    projectProperties.put("base", "java");
    projectProperties.put("name", "guenther");
    projectProperties.put("age", "42");
    projectProperties.put("ext", "png");

    Settings settings = new Settings();
    ArtifactRepository localRepository = new MavenArtifactRepository() {
        public String getBasedir() {
            return "repository";
        }
    };
    @SuppressWarnings("deprecation")
    MavenSession session = new MavenSession(null, settings, localRepository, null, null, Collections.<String>emptyList(), ".", null, null, new Date(System.currentTimeMillis()));
    session.getUserProperties().setProperty("cliOverride", "cliValue"); // Maven CLI override: -DcliOverride=cliValue
    session.getSystemProperties().put("user.name", "somebody"); // Java system property: -Duser.name=somebody
    return new MojoParameters(session, project, null, null, null, settings, "src", "target", Collections.singletonList(project));
}
 
Example #5
Source File: RepositoryUtils.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
public static ArtifactRepository convertFromMavenSettingsRepository(Repository repository)
{
   MavenArtifactRepository result = new MavenArtifactRepository();
   result.setId(repository.getId());
   result.setUrl(repository.getUrl());

   String layout = repository.getLayout();
   if ("default".equals(layout))
      result.setLayout(new DefaultRepositoryLayout());
   else if ("p2".equals(layout))
      result.setLayout(new P2ArtifactRepositoryLayout());

   RepositoryPolicy releases = repository.getReleases();
   if (releases != null)
      result.setReleaseUpdatePolicy(new ArtifactRepositoryPolicy(releases.isEnabled(), releases.getUpdatePolicy(),
               releases.getChecksumPolicy()));

   RepositoryPolicy snapshots = repository.getSnapshots();
   if (snapshots != null)
      result.setSnapshotUpdatePolicy(new ArtifactRepositoryPolicy(snapshots.isEnabled(),
               snapshots.getUpdatePolicy(),
               snapshots.getChecksumPolicy()));

   return result;
}
 
Example #6
Source File: RepositoryUtils.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
public static ArtifactRepository toArtifactRepository(String id, String url, String layout,
         boolean containsReleases,
         boolean containsSnapshots)
{
   MavenArtifactRepository result = new MavenArtifactRepository();
   result.setId(id);
   result.setUrl(url);

   if (layout == null || layout.isEmpty() || "default".equals(layout))
   {
      result.setLayout(new DefaultRepositoryLayout());
   }
   else if ("p2".equals(layout))
   {
      result.setLayout(new P2ArtifactRepositoryLayout());
   }
   result.setReleaseUpdatePolicy(new ArtifactRepositoryPolicy(containsReleases,
            ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
            ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN));
   result.setSnapshotUpdatePolicy(new ArtifactRepositoryPolicy(containsSnapshots,
            ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
            ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN));
   return result;

}
 
Example #7
Source File: DependencyResolver.java    From spring-init with Apache License 2.0 5 votes vote down vote up
private ArtifactRepository repo(String id, String url, boolean releases,
		boolean snapshots) {
	MavenArtifactRepository repository = new MavenArtifactRepository();
	repository.setLayout(new DefaultRepositoryLayout());
	repository.setId(id);
	repository.setUrl(url);
	ArtifactRepositoryPolicy enabled = new ArtifactRepositoryPolicy();
	enabled.setEnabled(true);
	ArtifactRepositoryPolicy disabled = new ArtifactRepositoryPolicy();
	disabled.setEnabled(false);
	repository.setReleaseUpdatePolicy(releases ? enabled : disabled);
	repository.setSnapshotUpdatePolicy(snapshots ? enabled : disabled);
	return repository;
}
 
Example #8
Source File: DependencyResolver.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private ArtifactRepository repo(String id, String url, boolean releases,
		boolean snapshots) {
	MavenArtifactRepository repository = new MavenArtifactRepository();
	repository.setLayout(new DefaultRepositoryLayout());
	repository.setId(id);
	repository.setUrl(url);
	ArtifactRepositoryPolicy enabled = new ArtifactRepositoryPolicy();
	enabled.setEnabled(true);
	ArtifactRepositoryPolicy disabled = new ArtifactRepositoryPolicy();
	disabled.setEnabled(false);
	repository.setReleaseUpdatePolicy(releases ? enabled : disabled);
	repository.setSnapshotUpdatePolicy(snapshots ? enabled : disabled);
	return repository;
}
 
Example #9
Source File: CreateEffectivePomTest.java    From flatten-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Tests method to create effective POM.
 *
 * @throws Exception if something goes wrong.
 */
@Test
public void testCreateEffectivePom()
    throws Exception
{

    String magicValue = "magic-value";
    Properties userProperties = new Properties();
    userProperties.setProperty( "cmd.test.property", magicValue );

    File pomFile = new File( "src/test/resources/cmdpropertysubstituion/pom.xml" );
    ArtifactRepository localRepository = new MavenArtifactRepository();
    localRepository.setLayout( new DefaultRepositoryLayout() );
    ArtifactFactory artifactFactory = new DefaultArtifactFactory();
    ArtifactHandlerManager artifactHandlerManager = new DefaultArtifactHandlerManager();
    setDeclaredField( artifactFactory, "artifactHandlerManager", artifactHandlerManager );
    Map<String, ArtifactHandler> artifactHandlers = new HashMap<String, ArtifactHandler>();
    setDeclaredField( artifactHandlerManager, "artifactHandlers", artifactHandlers );
    DefaultDependencyResolver depencencyResolver = new DefaultDependencyResolver();
    DefaultProjectBuildingRequest projectBuildingRequest = new DefaultProjectBuildingRequest();
    FlattenModelResolver resolver = new FlattenModelResolver( localRepository, artifactFactory,
            depencencyResolver, projectBuildingRequest, Collections.<MavenProject>emptyList() );
    ModelBuildingRequest buildingRequest =
        new DefaultModelBuildingRequest().setPomFile( pomFile ).setModelResolver( resolver ).setUserProperties( userProperties );
    setDeclaredField( tested, "modelBuilderThreadSafetyWorkaround", buildModelBuilderThreadSafetyWorkaroundForTest() );
    Model effectivePom = tested.createEffectivePom( buildingRequest, false, FlattenMode.defaults );
    assertThat( effectivePom.getName() ).isEqualTo( magicValue );
}
 
Example #10
Source File: DockerAssemblyManagerTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private MojoParameters mockMojoParams(MavenProject project) {
    Settings settings = new Settings();
    ArtifactRepository localRepository = new MavenArtifactRepository() {
        @Mock
        public String getBasedir() {
            return "repository";
        }
    };
    @SuppressWarnings("deprecation")
    MavenSession session = new MavenSession(null, settings, localRepository, null, null, Collections.<String>emptyList(), ".", null, null, new Date());
    return new MojoParameters(session, project, null, null, null, settings, "src", "target", Collections.singletonList(project));
}
 
Example #11
Source File: VersioningCalculatorTest.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings( "deprecation" )
private VersioningState setupSession( final Properties properties, final Map<ProjectRef, String[]> versionMap )
    throws Exception
{
    // Originally the default used to be 0, this was changed to be 5 but this affects this test suite so revert
    // just for these tests.
    if ( ! properties.containsKey( VersioningState.INCREMENT_SERIAL_SUFFIX_PADDING_SYSPROP ) )
    {
        properties.setProperty( VersioningState.INCREMENT_SERIAL_SUFFIX_PADDING_SYSPROP, "0" );
    }

    final ArtifactRepository ar =
        new MavenArtifactRepository( "test", TestUtils.MVN_CENTRAL, new DefaultRepositoryLayout(),
                                     new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );

    final MavenExecutionRequest req =
        new DefaultMavenExecutionRequest().setUserProperties( properties )
                                          .setRemoteRepositories( Collections.singletonList( ar ) );

    final PlexusContainer container = new DefaultPlexusContainer();

    final MavenSession mavenSession = new MavenSession( container, null, req, new DefaultMavenExecutionResult() );

    session = new ManipulationSession();
    session.setMavenSession( mavenSession );

    final VersioningState state = new VersioningState( properties );
    session.setState( state );

    final Map<String, byte[]> dataMap = new HashMap<>();
    if ( versionMap != null && !versionMap.isEmpty() )
    {
        for ( final Map.Entry<ProjectRef, String[]> entry : versionMap.entrySet() )
        {
            final String path = toMetadataPath( entry.getKey() );
            final byte[] data = setupMetadataVersions( entry.getValue() );
            dataMap.put( path, data );
        }
    }

    final Location mdLoc = MavenLocationExpander.EXPANSION_TARGET;
    final Transport mdTrans = new StubTransport( dataMap );

    modder =
        new TestVersionCalculator( new ManipulationSession(), mdLoc, mdTrans, temp.newFolder( "galley-cache" ) );

    return state;
}
 
Example #12
Source File: MavenLocationExpander.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
private void addSettingsProfileRepositoriesTo( final Set<Location> locs, final Settings settings,
                                               final List<String> activeProfiles,
                                               final MirrorSelector mirrorSelector )
    throws MalformedURLException
{
    if ( settings != null )
    {
        final Map<String, Profile> profiles = settings.getProfilesAsMap();
        if ( profiles != null && activeProfiles != null && !activeProfiles.isEmpty() )
        {
            final LinkedHashSet<String> active = new LinkedHashSet<>( activeProfiles );

            final List<String> settingsActiveProfiles = settings.getActiveProfiles();
            if ( settingsActiveProfiles != null && !settingsActiveProfiles.isEmpty() )
            {
                active.addAll( settingsActiveProfiles );
            }

            for ( final String profileId : active )
            {
                final Profile profile = profiles.get( profileId );
                if ( profile != null )
                {
                    final List<Repository> repositories = profile.getRepositories();
                    if ( repositories != null )
                    {
                        final List<Mirror> mirrors = settings.getMirrors();
                        final ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
                        for ( final Repository repo : repositories )
                        {
                            String id = repo.getId();
                            String url = repo.getUrl();

                            if ( mirrors != null )
                            {
                                final ArtifactRepositoryPolicy snapshots = convertPolicy( repo.getSnapshots() );
                                final ArtifactRepositoryPolicy releases = convertPolicy( repo.getReleases() );

                                final MavenArtifactRepository arepo =
                                    new MavenArtifactRepository( id, url, layout, snapshots, releases );

                                final Mirror mirror =
                                    mirrorSelector == null ? null : mirrorSelector.getMirror( arepo, mirrors );

                                if ( mirror != null )
                                {
                                    id = mirror.getId();
                                    url = mirror.getUrl();
                                }

                                SimpleHttpLocation addition = new SimpleHttpLocation( id, url, snapshots.isEnabled(), releases.isEnabled(), true, false, null );

                                addition.setAttribute(Location.CONNECTION_TIMEOUT_SECONDS, 60);

                                locs.add (addition);
                            }
                        }
                    }

                }
            }
        }
    }
}
 
Example #13
Source File: MavenLocationExpanderTest.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
@Test
public void mirrorAdjustsLocationURLs()
    throws Exception
{
    final Mirror mirror = new Mirror();
    mirror.setId( "test-mirror" );
    mirror.setMirrorOf( "*" );
    mirror.setUrl( "http://nowhere.com" );

    final ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();

    final ArtifactRepositoryPolicy snapshots =
        new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
                                      ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );

    final ArtifactRepositoryPolicy releases =
        new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                                      ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );

    final File localRepo = File.createTempFile( "local.repo.", ".dir" );
    localRepo.deleteOnExit();

    final ArtifactRepository local =
        new MavenArtifactRepository( "local", localRepo.toURI()
                                                       .toString(), layout, snapshots, releases );

    final ArtifactRepository remote =
        new MavenArtifactRepository( "remote", "http:///repo.maven.apache.org/maven2", layout, snapshots, releases );

    final Settings settings = new Settings();
    settings.addMirror( mirror );

    final MavenLocationExpander ex =
        new MavenLocationExpander( Collections.<Location> emptyList(),
                                   Collections.<ArtifactRepository> singletonList( remote ), local,
                                   new DefaultMirrorSelector(), settings, Collections.<String> emptyList() );

    final List<Location> result = ex.expand( MavenLocationExpander.EXPANSION_TARGET );

    assertThat( result.size(), equalTo( 2 ) );

    final Iterator<Location> iterator = result.iterator();
    Location loc = iterator.next();

    assertThat( loc.getName(), equalTo( local.getId() ) );
    assertThat( loc.getUri(), equalTo( local.getUrl() ) );

    loc = iterator.next();

    assertThat( loc.getName(), equalTo( mirror.getId() ) );
    assertThat( loc.getUri(), equalTo( mirror.getUrl() ) );
}
 
Example #14
Source File: MavenLocationExpanderTest.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
@Test
public void useActiveSettingsProfileRepos()
    throws Exception
{
    final ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();

    final ArtifactRepositoryPolicy snapshots =
        new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
                                      ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );

    final ArtifactRepositoryPolicy releases =
        new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                                      ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );

    final File localRepo = File.createTempFile( "local.repo.", ".dir" );
    localRepo.deleteOnExit();

    final ArtifactRepository local =
        new MavenArtifactRepository( "local", localRepo.toURI()
                                                       .toString(), layout, snapshots, releases );

    final Repository remote = new Repository();
    remote.setId( "remote" );
    remote.setUrl( "http:///repo.maven.apache.org/maven2" );

    final Profile profile = new Profile();
    profile.setId( "test" );
    profile.addRepository( remote );

    final Settings settings = new Settings();
    settings.addProfile( profile );

    final MavenLocationExpander ex =
        new MavenLocationExpander( Collections.<Location> emptyList(),
                                   Collections.<ArtifactRepository> emptyList(), local,
                                   new DefaultMirrorSelector(), settings,
                                   Collections.<String> singletonList( profile.getId() ) );

    final List<Location> result = ex.expand( MavenLocationExpander.EXPANSION_TARGET );

    assertThat( result.size(), equalTo( 2 ) );

    final Iterator<Location> iterator = result.iterator();
    Location loc = iterator.next();

    assertThat( loc.getName(), equalTo( local.getId() ) );
    assertThat( loc.getUri(), equalTo( local.getUrl() ) );

    loc = iterator.next();

    assertThat( loc.getName(), equalTo( remote.getId() ) );
    assertThat( loc.getUri(), equalTo( remote.getUrl() ) );
}
 
Example #15
Source File: MavenLocationExpanderTest.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
@Test
public void locationURLs()
                throws Exception
{
    final ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();

    final ArtifactRepositoryPolicy snapshots =
                    new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
                                                  ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );

    final ArtifactRepositoryPolicy releases =
                    new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                                                  ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );

    final File localRepo = File.createTempFile( "local.repo.", ".dir" );
    localRepo.deleteOnExit();

    final ArtifactRepository local =
                    new MavenArtifactRepository( "local", localRepo.toURI()
                                                                   .toString(), layout, snapshots, releases );

    final ArtifactRepository remote =
                    new MavenArtifactRepository( "remote", "http:///repo.maven.apache.org/maven2", layout, snapshots, releases );

    final Settings settings = new Settings();

    final MavenLocationExpander ex =
                    new MavenLocationExpander( Collections.<Location> emptyList(),
                                               Collections.<ArtifactRepository> singletonList( remote ), local,
                                               new DefaultMirrorSelector(), settings, Collections.<String> emptyList() );

    final List<Location> result = ex.expand( MavenLocationExpander.EXPANSION_TARGET );

    assertThat( result.size(), equalTo( 2 ) );

    final Iterator<Location> iterator = result.iterator();
    Location loc = iterator.next();

    assertThat( loc.getName(), equalTo( local.getId() ) );
    assertThat( loc.getUri(), equalTo( local.getUrl() ) );

    loc = iterator.next();

    assertThat( loc.getName(), equalTo( remote.getId() ) );
    assertThat( loc.getUri(), equalTo( remote.getUrl() ) );

}
 
Example #16
Source File: P2ArtifactRepositoryLayout.java    From furnace with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public ArtifactRepository newMavenArtifactRepository(String id, String url, ArtifactRepositoryPolicy snapshots,
         ArtifactRepositoryPolicy releases)
{
   return new MavenArtifactRepository(id, url, this, DISABLED_POLICY, DISABLED_POLICY);
}