use of org.apache.maven.settings.Server in project bazel by bazelbuild.
the class MavenDownloader method download.
/**
* Download the Maven artifact to the output directory. Returns the path to the jar.
*/
public Path download(String name, WorkspaceAttributeMapper mapper, Path outputDirectory, MavenServerValue serverValue) throws IOException, EvalException {
this.name = name;
this.outputDirectory = outputDirectory;
String url = serverValue.getUrl();
Server server = serverValue.getServer();
Artifact artifact;
String artifactId = mapper.get("artifact", Type.STRING);
String sha1 = mapper.isAttributeValueExplicitlySpecified("sha1") ? mapper.get("sha1", Type.STRING) : null;
if (sha1 != null && !KeyType.SHA1.isValid(sha1)) {
throw new IOException("Invalid SHA-1 for maven_jar " + name + ": '" + sha1 + "'");
}
try {
artifact = new DefaultArtifact(artifactId);
} catch (IllegalArgumentException e) {
throw new IOException(e.getMessage());
}
boolean isCaching = repositoryCache.isEnabled() && KeyType.SHA1.isValid(sha1);
if (isCaching) {
Path downloadPath = getDownloadDestination(artifact);
Path cachedDestination = repositoryCache.get(sha1, downloadPath, KeyType.SHA1);
if (cachedDestination != null) {
return cachedDestination;
}
}
MavenConnector connector = new MavenConnector(outputDirectory.getPathString());
RepositorySystem system = connector.newRepositorySystem();
RepositorySystemSession session = connector.newRepositorySystemSession(system);
RemoteRepository repository = new RemoteRepository.Builder(name, MavenServerValue.DEFAULT_ID, url).setAuthentication(new MavenAuthentication(server)).build();
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(ImmutableList.of(repository));
try {
ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
artifact = artifactResult.getArtifact();
} catch (ArtifactResolutionException e) {
throw new IOException("Failed to fetch Maven dependency: " + e.getMessage());
}
Path downloadPath = outputDirectory.getRelative(artifact.getFile().getAbsolutePath());
// Verify checksum.
if (!Strings.isNullOrEmpty(sha1)) {
RepositoryCache.assertFileChecksum(sha1, downloadPath, KeyType.SHA1);
}
if (isCaching) {
repositoryCache.put(sha1, downloadPath, KeyType.SHA1);
}
return downloadPath;
}
use of org.apache.maven.settings.Server in project karaf by apache.
the class MavenConfigurationSupport method readSettings.
/**
* Reads on demand <code>settings.xml</code> file - without password decryption. Also
* collects declared servers by ID.
* @param settingsFile
*/
protected synchronized Settings readSettings(File settingsFile) throws SettingsBuildingException {
if (!settingsFile.isFile() || !settingsFile.canRead()) {
return null;
}
try {
DefaultSettingsBuilderFactory factory = new DefaultSettingsBuilderFactory();
DefaultSettingsBuilder builder = factory.newInstance();
SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
request.setUserSettingsFile(settingsFile);
SettingsBuildingResult result = builder.build(request);
if (result.getProblems().size() > 0) {
for (SettingsProblem problem : result.getProblems()) {
System.err.println(problem);
}
return null;
} else {
Settings settings = result.getEffectiveSettings();
if (settings.getServers() != null) {
for (Server server : settings.getServers()) {
servers.put(server.getId(), server);
}
}
return settings;
}
} catch (Throwable e) {
System.err.println(e.getMessage());
LOG.error(e.getMessage(), e);
throw e;
}
}
use of org.apache.maven.settings.Server in project karaf by apache.
the class RepositoryEditCommandSupport method updateCredentials.
/**
* Stores credential information in settings, without persisting them
* @param force
* @param id
* @param username
* @param password
* @param prefix
* @param config
*/
protected boolean updateCredentials(boolean force, String id, String username, String password, String prefix, Dictionary<String, Object> config) throws IOException {
if (!force && !confirm("Maven settings will be updated and org.ops4j.pax.url.mvn.settings property will change. Continue? (y/N) ")) {
return false;
}
Optional<Server> existingServer = mavenSettings.getServers().stream().filter((s) -> id.equals(s.getId())).findAny();
Server server = null;
if (existingServer.isPresent()) {
server = existingServer.get();
} else {
server = new Server();
server.setId(id);
mavenSettings.getServers().add(server);
}
server.setUsername(username);
server.setPassword(password);
return true;
}
use of org.apache.maven.settings.Server in project karaf by apache.
the class RepositoryListCommand method doAction.
@Override
public void doAction(String prefix, Dictionary<String, Object> config) throws Exception {
ShellTable table = new ShellTable();
table.column("ID");
table.column("URL");
if (verbose) {
table.column("Releases");
table.column("Snapshots");
table.column("Defined in");
}
if (showPasswords) {
table.column("Username");
table.column("Password");
}
System.out.println();
System.out.println("== Remote repositories");
MavenRepositoryURL[] repositories = repositories(config, true);
for (MavenRepositoryURL repoURL : repositories) {
Row row = table.addRow();
row.addContent(repoURL.getId(), repoURL.getURL());
if (verbose) {
row.addContent(repositoryKindInfo(repoURL, false), repositoryKindInfo(repoURL, true), repoURL.getFrom());
}
if (showPasswords) {
if (servers.containsKey(repoURL.getId())) {
Server server = servers.get(repoURL.getId());
row.addContent(server.getUsername() == null ? "" : server.getUsername());
addPasswordInfo(row, serverPasswords, repoURL.getId(), server.getPassword());
} else {
row.addContent("", "");
}
}
}
table.print(System.out);
table = new ShellTable();
table.column("ID");
table.column("URL");
if (verbose) {
table.column("Releases");
table.column("Snapshots");
}
System.out.println();
System.out.println("== Default repositories");
repositories = repositories(config, false);
for (MavenRepositoryURL repoURL : repositories) {
Row row = table.addRow();
row.addContent(repoURL.getId(), repoURL.getURL());
if (verbose) {
row.addContent(repositoryKindInfo(repoURL, false), repositoryKindInfo(repoURL, true), repoURL.getFrom());
}
}
table.print(System.out);
System.out.println();
}
use of org.apache.maven.settings.Server in project karaf by apache.
the class SettingsTest method readAndWriteSettings.
@Test
public void readAndWriteSettings() throws IOException {
Settings settings = new Settings();
Server s = new Server();
s.setId("id");
s.setUsername("admin");
s.setPassword("admin");
settings.getServers().add(s);
new DefaultSettingsWriter().write(System.out, null, settings);
}
Aggregations