Skip to content

Commit

Permalink
Inject content of settings.xml into artifact repositories.
Browse files Browse the repository at this point in the history
For artifact repositories which have been specified in the target
platform definition, Tycho creates a minimal instance, containing only
an id and url, which is then injected back into the Maven model.

In order to support e.g. authenticating against a private repository, it
is necessary to also inject the user credentials, provided via the local
settings.xml, into this instance. Otherwise accessing the referenced
artifacts fails with a 401 - Unauthorized.
  • Loading branch information
ptziegler authored and laeubi committed Sep 15, 2022
1 parent df2fbc7 commit dc5a5e8
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.tycho.ArtifactDescriptor;
Expand Down Expand Up @@ -66,7 +67,7 @@ public final class MavenDependencyInjector {
public static void injectMavenDependencies(MavenProject project, DependencyArtifacts dependencies,
DependencyArtifacts testDependencies, BundleReader bundleReader,
Function<ArtifactDescriptor, MavenDependencyDescriptor> descriptorMapping, Logger logger,
RepositorySystem repositorySystem) {
RepositorySystem repositorySystem, Settings settings) {
MavenDependencyInjector generator = new MavenDependencyInjector(project, bundleReader, descriptorMapping,
logger);
for (ArtifactDescriptor artifact : dependencies.getArtifacts()) {
Expand Down Expand Up @@ -97,7 +98,11 @@ public static void injectMavenDependencies(MavenProject project, DependencyArtif
+ reference.getUrl() + ", existing URL = " + artifactRepository.getUrl());
}
}
project.setRemoteArtifactRepositories(new ArrayList<>(repositoryMap.values()));
List<ArtifactRepository> artifactRepositories = new ArrayList<>(repositoryMap.values());
repositorySystem.injectMirror(artifactRepositories, settings.getMirrors());
repositorySystem.injectProxy(artifactRepositories, settings.getProxies());
repositorySystem.injectAuthentication(artifactRepositories, settings.getServers());
project.setRemoteArtifactRepositories(artifactRepositories);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public boolean include(Artifact a) {
} else {
request.setRemoteRepositories(mavenSession.getCurrentProject().getRemoteArtifactRepositories());
}
repositorySystem.injectMirror(request.getRemoteRepositories(), mavenSession.getSettings().getMirrors());
repositorySystem.injectProxy(request.getRemoteRepositories(), mavenSession.getSettings().getProxies());
repositorySystem.injectAuthentication(request.getRemoteRepositories(), mavenSession.getSettings().getServers());
ArtifactResolutionResult result = repositorySystem.resolve(request);
if (result.hasExceptions()) {
throw new DependencyResolutionException("resolving " + artifact + " failed!", result.getExceptions());
Expand Down
44 changes: 44 additions & 0 deletions tycho-its/projects/target.maven.httpAuthentication/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>tycho-its-project</groupId>
<artifactId>target.maven.httpAuthentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<tycho-version>3.0.0-SNAPSHOT</tycho-version>
</properties>

<modules>
<module>target.test</module>
<module>test.bundle</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<target>
<artifact>
<groupId>${groupId}</groupId>
<artifactId>target-definition</artifactId>
<version>0.0.1-SNAPSHOT</version>
</artifact>
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<settings>
<servers>
<server>
<id>test-server</id>
<username>test-user</username>
<password>test-password</password>
</server>
</servers>
</settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.5"?>
<target name="platform">
<locations>

<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" label="DSA 3pps" missingManifest="generate" type="Maven">
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>bundle</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
</dependencies>
<repositories>
<repository>
<id>test-server</id>
<url>https://nexusext.app.corp.dsa.de/repository/sfp-alp-eal-group-releases/</url>
</repository>
</repositories>
</location>
</locations>
</target>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>target-definition</artifactId>
<packaging>eclipse-target-definition</packaging>

<parent>
<groupId>tycho-its-project</groupId>
<artifactId>target.maven.httpAuthentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bundle Plug-in
Bundle-SymbolicName: test.bundle
Bundle-Version: 0.0.1.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>test.bundle</artifactId>
<packaging>eclipse-plugin</packaging>

<parent>
<groupId>tycho-its-project</groupId>
<artifactId>target.maven.httpAuthentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
Binary file not shown.
19 changes: 19 additions & 0 deletions tycho-its/repositories/m2/test/bundle/1.0.0/bundle-1.0.0.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>bundle</artifactId>
<version>1.0.0</version>
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>2.7.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2022 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Patrick Ziegler - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.test.target;

import java.io.File;
import java.util.Properties;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.eclipse.tycho.test.util.HttpServer;
import org.eclipse.tycho.test.util.ResourceUtil;
import org.eclipse.tycho.test.util.TargetDefinitionUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class PasswordProtectedM2RepositoryTest extends AbstractTychoIntegrationTest {

private HttpServer server;
private String m2RepoUrl;

@Before
public void startServer() throws Exception {
server = HttpServer.startServer("test-user", "test-password");
m2RepoUrl = server.addServer("foo", ResourceUtil.resolveTestResource("repositories/m2"));
}

@After
public void stopServer() throws Exception {
server.stop();
}

@Test
public void testTargetDefinition() throws Exception {
Verifier verifier = createVerifier("settings.xml");
File platformBundle = new File(verifier.getBasedir(), "target.test");
File platformFile = new File(platformBundle, "platform.target");
TargetDefinitionUtil.setRepositoryURLs(platformFile, m2RepoUrl);
verifier.executeGoal("package");
verifier.verifyErrorFreeLog();
}

private Verifier createVerifier(String settingsFile) throws Exception {
Verifier verifier = getVerifier("target.maven.httpAuthentication", false,
new File("projects/target.maven.httpAuthentication/" + settingsFile));
Properties systemProperties = verifier.getSystemProperties();
systemProperties.setProperty("m2.repo", m2RepoUrl);
return verifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public static void setRepositoryURLs(File targetDefinitionFile, String url)
for (int i = 0; i < repositories.getLength(); i++) {
Element repository = (Element) repositories.item(i);
repository.setAttribute("location", url);
NodeList urls = repository.getElementsByTagName("url");
// m2 repositories use an id and url tag
for (int j = 0; j < urls.getLength(); ++j) {
urls.item(j).setTextContent(url);
}
}
}
try (FileOutputStream output = new FileOutputStream(targetDefinitionFile)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
Expand Down Expand Up @@ -130,6 +131,9 @@ public class P2DependencyResolver extends AbstractLogEnabled implements Dependen
@Requirement
private PluginRealmHelper pluginRealmHelper;

@Requirement
private LegacySupport context;

private P2ResolverFactory resolverFactory;

private DependencyMetadataGenerator generator;
Expand Down Expand Up @@ -500,6 +504,7 @@ public void initialize() throws InitializationException {
public void injectDependenciesIntoMavenModel(MavenProject project, AbstractTychoProject projectType,
DependencyArtifacts dependencyArtifacts, DependencyArtifacts testDependencyArtifacts, Logger logger) {
MavenDependencyInjector.injectMavenDependencies(project, dependencyArtifacts, testDependencyArtifacts,
bundleReader, resolverFactory::resolveDependencyDescriptor, logger, repositorySystem);
bundleReader, resolverFactory::resolveDependencyDescriptor, logger, repositorySystem,
context.getSession().getSettings());
}
}

0 comments on commit dc5a5e8

Please sign in to comment.