Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
feature(artifactresolver): Use additional classifier and repo
Browse files Browse the repository at this point in the history
- this allows to fill a reference repository with updated sources
- this repository can be configured in the pom
- in addition, a classifier can be configured in the workflow step

Signed-off-by: Martin Idel <external.Martin.Idel@bosch-si.com>
  • Loading branch information
Martin-Idel-SI committed Apr 12, 2019
1 parent a82266b commit 4d97293
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class ToolConfiguration {
private String version;
private Path scanDir;
private boolean isMavenInstalled;
private String sourcesRepositoryUrl;
private String companyName;
private String copyrightHoldersName;
private String copyrightNotice;
Expand Down Expand Up @@ -76,7 +75,6 @@ public ToolConfiguration(ConfigurationBuilder builder) {
this.version = builder.version;
this.scanDir = builder.scanDir;
this.isMavenInstalled = builder.isMavenInstalled;
this.sourcesRepositoryUrl = builder.sourcesRepositoryUrl;
this.companyName = builder.companyName;
this.copyrightHoldersName = builder.copyrightHoldersName;
this.copyrightNotice = builder.copyrightNotice;
Expand Down Expand Up @@ -144,10 +142,6 @@ public boolean isMavenInstalled() {
return isMavenInstalled;
}

public String getSourcesRepositoryUrl() {
return sourcesRepositoryUrl;
}

public String getCompanyName() {
return companyName;
}
Expand Down Expand Up @@ -201,7 +195,6 @@ public static class ConfigurationBuilder {
private String version;
private Path scanDir;
private boolean isMavenInstalled;
private String sourcesRepositoryUrl;
private String companyName;
private String copyrightHoldersName;
private String copyrightNotice;
Expand Down Expand Up @@ -278,11 +271,6 @@ public ConfigurationBuilder setMavenInstalled(boolean mavenInstalled) {
return this;
}

public ConfigurationBuilder setSourcesRepositoryUrl(String sourcesRepositoryUrl) {
this.sourcesRepositoryUrl = sourcesRepositoryUrl;
return this;
}

public ConfigurationBuilder setCompanyName(String companyName) {
this.companyName = companyName;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@ Add the following step into the `<processors>` section of your workflow.xml
<step>
<name>Maven Artifact Resolver</name>
<classHint>org.eclipse.sw360.antenna.workflow.processors.enricher.MavenArtifactResolver</classHint>
<configuration>
<entry key="sourcesRepositoryUrl" value="https://my.url.to/repo"/>
<entry key="preferredSourceClassifier" value="sources-ext"/>
</configuration>
</step>
```
```

#### Explanation of parameters

- `sourcesRepositoryUrl`: *(optional)* valid URL to maven repository (e.g. a company nexus) containing additional source jars for resolution.
- `preferredSourceQualifier`: *(optional)* will be used by the artifact resolver as a qualifier for source jars before tying the usual qualifier `sources`.
This should be used together with `sourcesRepositoryUrl` providing a repository to search for artifacts with the given qualifier.

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ Defaults is false for Gradle and CLI.
For Maven it the variable is ignored.
* `${docName}TargetDirectory`: *(optional)* The folder where the documents created by ${docNameCap} are stored.
Defaults to a folder called ${docNameCap} inside the build folder of the project.
* `sourcesRepositoryUrl`: *(deprecated)* Is already ignored by the project.
* `sourcesRepositoryUrl`: *(deprecated)* This option once allowed to set another remote repository address
However, in the current version it does not do anything and will be removed soon.

**Related to attaching artifacts**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ private ToolConfiguration loadConfiguration() throws AntennaExecutionException {
.setVersion(version).setScanDir(scanDir)
.setSkipAntennaExecution(skip)
.setMavenInstalled(true) // when using the maven plugin, maven is surely available
.setSourcesRepositoryUrl(sourcesRepositoryUrl).setCompanyName(companyName)
.setCopyrightHoldersName(copyrightHoldersName).setCopyrightNotice(copyrightNotice)
.setDisclosureDocumentNotes(disclosureDocumentNotes).setWorkflow(finalWorkflow)
.setProxyHost(proxyHost).setProxyPort(proxyPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<artifactId>maven-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,46 @@
import org.apache.maven.repository.RepositorySystem;
import org.eclipse.sw360.antenna.api.configuration.AntennaContext;
import org.eclipse.sw360.antenna.api.configuration.ToolConfiguration;
import org.eclipse.sw360.antenna.api.exceptions.AntennaException;

import java.util.List;
import java.net.URL;
import java.util.Optional;

/**
* Returns classes for requesting the jars of Artifacts.
*/
public class ArtifactRequesterFactory {
public static IArtifactRequester getArtifactRequester(AntennaContext context) {
public static IArtifactRequester getArtifactRequester(AntennaContext context, URL sourcesRepositoryUrl) throws AntennaException {
ToolConfiguration toolConfig = context.getToolConfiguration();

if (toolConfig.isMavenInstalled()) {
return useMavenIfRunning(context).orElse(new MavenInvokerRequester(context));
return useMavenIfRunning(context, Optional.of(sourcesRepositoryUrl)).orElse(new MavenInvokerRequester(context, sourcesRepositoryUrl));
}
return new HttpRequester(context, sourcesRepositoryUrl);
}

public static IArtifactRequester getArtifactRequester(AntennaContext context) throws AntennaException {
ToolConfiguration toolConfig = context.getToolConfiguration();

if (toolConfig.isMavenInstalled()) {
return useMavenIfRunning(context, Optional.empty()).orElse(new MavenInvokerRequester(context));
}
return new HttpRequester(context);
}

/*
* Must only be used if Maven installation can be found on system, will result in ClassNotFoundError otherwise
*/
private static Optional<IArtifactRequester> useMavenIfRunning(AntennaContext context) {
private static Optional<IArtifactRequester> useMavenIfRunning(AntennaContext context, Optional<URL> sourcesRepositoryUrl) {
Optional<RepositorySystem> optionalRepositorySystem = context.getGeneric(RepositorySystem.class);
Optional<MavenProject> optionalMavenProject = context.getGeneric(MavenProject.class);
Optional<LegacySupport> optionalLegacySupport = context.getGeneric(LegacySupport.class);
if (optionalRepositorySystem.isPresent() &&
optionalMavenProject.isPresent() &&
optionalLegacySupport.isPresent()) {
List<ArtifactRepository> remoteRepositories = optionalMavenProject.get().getRemoteArtifactRepositories();
ArtifactRepository localRepository = optionalLegacySupport.get().getSession().getLocalRepository();

if (localRepository != null) {
return Optional.of(new MavenRuntimeRequester(context, optionalRepositorySystem.get(), localRepository, remoteRepositories));
return Optional.of(new MavenRuntimeRequester(context, optionalRepositorySystem.get(), localRepository, optionalMavenProject.get().getRemoteArtifactRepositories(), sourcesRepositoryUrl));
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) Bosch Software Innovations GmbH 2019.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.antenna.bundle;

public class ClassifierInformation {
public static final ClassifierInformation DEFAULT_JAR = new ClassifierInformation("", false);
public static final ClassifierInformation DEFAULT_SOURCE_JAR = new ClassifierInformation("sources", true);
public final String classifier;
public final boolean isSource;

public ClassifierInformation(String classifier, boolean isSource) {
this.classifier = classifier;
this.isSource = isSource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
*/
package org.eclipse.sw360.antenna.bundle;

import org.apache.commons.lang.StringUtils;
import org.eclipse.sw360.antenna.api.configuration.AntennaContext;
import org.eclipse.sw360.antenna.api.configuration.ToolConfiguration;
import org.eclipse.sw360.antenna.exceptions.FailedToDownloadException;
import org.eclipse.sw360.antenna.model.artifact.facts.java.MavenCoordinates;
import org.eclipse.sw360.antenna.util.HttpHelper;
Expand All @@ -21,6 +19,7 @@

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Optional;

Expand All @@ -29,41 +28,71 @@
*/
public class HttpRequester extends IArtifactRequester {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequester.class);
public static final String GROUP_ID_PLACEHOLDER = "{groupId}";
public static final String ARTIFACT_ID_PLACEHOLDER = "{artifactId}";
public static final String VERSION_PLACEHOLDER = "{version}";
private static final String GROUP_ID_PLACEHOLDER = "{groupId}";
private static final String ARTIFACT_ID_PLACEHOLDER = "{artifactId}";
private static final String VERSION_PLACEHOLDER = "{version}";
private static final String MAVEN_CENTRAL_URL = "http://repo2.maven.org/maven2/" + GROUP_ID_PLACEHOLDER + "/" + ARTIFACT_ID_PLACEHOLDER + "/" + VERSION_PLACEHOLDER + "/";

private HttpHelper httpHelper;
private Optional<URL> sourceRepositoryUrl;

public HttpRequester(AntennaContext context, URL sourceRepositoryUrl) {
super(context);
httpHelper = new HttpHelper(context);
this.sourceRepositoryUrl = Optional.of(sourceRepositoryUrl);
}

public HttpRequester(AntennaContext context) {
super(context);
httpHelper = new HttpHelper(context);
this.sourceRepositoryUrl = Optional.empty();
}

@Override
public Optional<File> requestFile(MavenCoordinates coordinates, Path targetDirectory, boolean isSource) {
String jarBaseName = getExpectedJarBaseName(coordinates, isSource);
public Optional<File> requestFile(MavenCoordinates coordinates, Path targetDirectory, ClassifierInformation classifierInformation) {
String jarBaseName = getExpectedJarBaseName(coordinates, classifierInformation);
File localJarFile = targetDirectory.resolve(jarBaseName).toFile();

if (localJarFile.exists()) {
LOGGER.info("The file " + localJarFile + " already exists and won't be downloaded again");
return Optional.of(localJarFile);
}

String jarUrl = getJarUrl(coordinates, jarBaseName);
Optional<File> downloadedFile = downloadFileFromUserUrl(coordinates, targetDirectory, jarBaseName);

if (!downloadedFile.isPresent()) {
String mavenCentralJarUrl = getJarUrl(coordinates, jarBaseName, MAVEN_CENTRAL_URL);
return tryFileDownload(mavenCentralJarUrl, targetDirectory, jarBaseName);
}
return downloadedFile;
}

private Optional<File> downloadFileFromUserUrl(MavenCoordinates coordinates, Path targetDirectory, String jarBaseName) {
if (sourceRepositoryUrl.isPresent()) {
String jarUrl = convertToJarUrlTemplate(coordinates, jarBaseName, sourceRepositoryUrl.get().toString());
return tryFileDownload(jarUrl, targetDirectory, jarBaseName);
}
return Optional.empty();
}

private String convertToJarUrlTemplate(MavenCoordinates coordinates, String jarBaseName, String repoTemplate) {
String enrichedTemplate = repoTemplate;
enrichedTemplate += repoTemplate.endsWith("/") ? "" : "/";
enrichedTemplate += GROUP_ID_PLACEHOLDER + "/" + ARTIFACT_ID_PLACEHOLDER + "/" + VERSION_PLACEHOLDER + "/";
return getJarUrl(coordinates, jarBaseName, enrichedTemplate);
}

private Optional<File> tryFileDownload(String jarUrl, Path targetDirectory, String jarBaseName) {
try {
LOGGER.info("HttpRequester with download path " + jarUrl);
return Optional.ofNullable(httpHelper.downloadFile(jarUrl, targetDirectory, jarBaseName));
} catch (FailedToDownloadException | IOException e) {
LOGGER.warn("Failed to find jar: " + e.getMessage(), e);
LOGGER.warn("Failed to find jar: " + e.getMessage());
return Optional.empty();
}
}

private String getJarUrl(MavenCoordinates coordinates, String remoteFileName) {
String repoTemplate = getJarUrlTemplate();

private String getJarUrl(MavenCoordinates coordinates, String remoteFileName, String repoTemplate) {
// Construct URL (substitute in groupID, artifactID and version
// NOTE: There should be no dots in the groupID. Dots delimit
// directories, so are converted to slashes.
Expand All @@ -73,31 +102,6 @@ private String getJarUrl(MavenCoordinates coordinates, String remoteFileName) {
.replace(ARTIFACT_ID_PLACEHOLDER, coordinates.getArtifactId())
.replace(VERSION_PLACEHOLDER, coordinates.getVersion());

if (!repo.substring(repo.length() - 1).equals("/")) {
repo = repo + "/";
}

return repo + remoteFileName;
}

private String getJarUrlTemplate() {
ToolConfiguration toolConfig = context.getToolConfiguration();
String repoTemplate = toolConfig.getSourcesRepositoryUrl();

if (StringUtils.isBlank(repoTemplate)) {
repoTemplate = MAVEN_CENTRAL_URL;
} else if (!isValidJarUrlTemplate(repoTemplate)) {
LOGGER.warn("The sourcesRepositoryUrl does not contain all the required placeholders: " +
GROUP_ID_PLACEHOLDER + ", " + ARTIFACT_ID_PLACEHOLDER + ", " + VERSION_PLACEHOLDER +
". Falling back to using Maven Central.");
repoTemplate = MAVEN_CENTRAL_URL;
}
return repoTemplate;
}

private boolean isValidJarUrlTemplate(String sourceRepoTemplate) {
return sourceRepoTemplate.contains(GROUP_ID_PLACEHOLDER)
&& sourceRepoTemplate.contains(ARTIFACT_ID_PLACEHOLDER)
&& sourceRepoTemplate.contains(VERSION_PLACEHOLDER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
public abstract class IArtifactRequester {

public static final String JAR_EXTENSION = ".jar";
public static final String SOURCES_JAR_EXTENSION = "-sources" + JAR_EXTENSION;
protected AntennaContext context;

public IArtifactRequester(AntennaContext context) {
Expand All @@ -31,16 +30,16 @@ public IArtifactRequester(AntennaContext context) {
/**
* Requests a jar file from a repository.
*
* @param coordinates Identifies the artifact for which the jar is requested.
* @param targetDirectory Where the jar file will be stored.
* @param isSource Whether the request should retrieve the sources.
* @param coordinates Identifies the artifact for which the jar is requested.
* @param targetDirectory Where the jar file will be stored.
* @param classifierInformation Information on the classifier (source or not and name).
* @return The jar file, or null if the file couldn't be obtained.
*/
public abstract Optional<File> requestFile(MavenCoordinates coordinates, Path targetDirectory, boolean isSource);
public abstract Optional<File> requestFile(MavenCoordinates coordinates, Path targetDirectory, ClassifierInformation classifierInformation);

String getExpectedJarBaseName(MavenCoordinates coordinates, boolean isSource) {
String getExpectedJarBaseName(MavenCoordinates coordinates, ClassifierInformation classifierInformation) {
return coordinates.getArtifactId() + "-" + coordinates.getVersion()
+ (isSource ? SOURCES_JAR_EXTENSION : JAR_EXTENSION);
+ (classifierInformation.classifier.isEmpty() ? JAR_EXTENSION : "-" + classifierInformation.classifier + JAR_EXTENSION);
}

}
Loading

0 comments on commit 4d97293

Please sign in to comment.