Skip to content

Commit

Permalink
SHRINKRES-9 Enabled explicit profile activation
Browse files Browse the repository at this point in the history
* Enabled explicit profile activation
* Enabled explicit profile disactivation
* Full activation support
  • Loading branch information
kpiwko committed Apr 23, 2012
1 parent 4912862 commit a0b4fae
Show file tree
Hide file tree
Showing 18 changed files with 693 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ public static File[] resolveAsFiles(String... coordinates) throws ResolutionExce
* resolve an artifact without explicitly specifying its version.
*
* @param path A path to the POM file, must not be {@code null} or empty
* @param profiles Allows user to specify which profiles will be activated. Note, profiles from settings.xml file are
* activated by default. If you want to disable a profile, use {@code !$ profile.name}} or {@code -$ profile.name}}
* syntax
* @return A dependency builder with remote repositories set according to the content of POM file.
*/
public static EffectivePomMavenDependencyShortcut withPom(String path) {
return DependencyResolvers.use(MavenDependencyShortcut.class).withPom(path);
public static EffectivePomMavenDependencyShortcut withPom(String path, String... profiles) {
return DependencyResolvers.use(MavenDependencyShortcut.class).withPom(path, profiles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ public interface MavenDependencyResolver extends ResolverEntryPoint<MavenDepende
* present in the POM file.
*
* @param path A path to the POM file, must not be {@code null} or empty
* @param profiles Allows user to specify which profiles will be activated. Note, profiles from settings.xml file are
* activated by default. If you want to disable a profile, use {@code !$ profile.name} or {@code -$ profile.name}
* syntax
* @return A dependency builder with remote repositories set according to the content of POM file.
* @throws ResolutionException If an effective POM cannot be resolved
*/
EffectivePomMavenDependencyResolver loadEffectivePom(String path) throws ResolutionException;
EffectivePomMavenDependencyResolver loadEffectivePom(String path, String... profiles) throws ResolutionException;

/**
* Disables touching of Maven Central repository. This repository is enabled by default in Maven
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
*
* @author <a href="http://community.jboss.org/people/silenius">Samuel Santos</a>
*/
public interface MavenDependencyShortcut extends ResolverEntryPoint<MavenDependencyShortcut>, EffectivePomMavenDependencyShortcut {
public interface MavenDependencyShortcut extends ResolverEntryPoint<MavenDependencyShortcut>,
EffectivePomMavenDependencyShortcut {

/**
* Loads remote repositories for a POM file. If repositories are defined in the parent of the POM file and there are
Expand All @@ -44,7 +45,10 @@ public interface MavenDependencyShortcut extends ResolverEntryPoint<MavenDepende
* resolve an artifact without explicitly specifying its version.
*
* @param path A path to the POM file, must not be {@code null} or empty
* @param profiles Allows user to specify which profiles will be activated. Note, profiles from settings.xml file are
* activated by default. If you want to disable a profile, use {@code !$ profile.name} or {@code -$ profile.name}
* syntax
* @return A dependency builder with remote repositories set according to the content of POM file.
*/
EffectivePomMavenDependencyShortcut withPom(String path);
EffectivePomMavenDependencyShortcut withPom(String path, String... profiles);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public interface MavenImporter extends Assignable {
* Loads effective pom from a given location. It will use profiles activated by default.
*
* @param path The path to the effective pom.
* @param profiles Allows user to specify which profiles will be activated. Note, profiles from settings.xml file are
* activated by default. If you want to disable a profile, use {@code !$ profile.name} or {@code -$ profile.name}
* syntax
* @return MavenImporter which is able to enrich current archive
*/
EffectivePomMavenImporter loadEffectivePom(String path);
EffectivePomMavenImporter loadEffectivePom(String path, String... profiles);

/**
* A ShrinkWrap importer which already has metadata required in order to modify the archive.
Expand Down Expand Up @@ -74,8 +77,8 @@ static interface EffectivePomMavenImporter extends Assignable {
EffectivePomMavenImporter importTestDependencies();

/**
* Adds all dependencies defined by a pom file in scope test. User have to use filtering for the dependencies.
* This is supported only for WAR and EAR packagings.
* Adds all dependencies defined by a pom file in scope test. User have to use filtering for the dependencies. This is
* supported only for WAR and EAR packagings.
*
* @param filter The filter to be applied
* @return The modified archive
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jboss.shrinkwrap.resolver.impl.maven;

import java.util.logging.Logger;

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.building.ModelProblem.Severity;
import org.apache.maven.model.building.ModelProblemCollector;

public class LogModelProblemCollector implements ModelProblemCollector {
private static final Logger log = Logger.getLogger(LogModelProblemCollector.class.getName());

private boolean hasSevereFailures;

public LogModelProblemCollector() {
this.hasSevereFailures = false;
}

@Override
public void add(Severity severity, String message, InputLocation location, Exception cause) {

switch (severity) {
case WARNING:
log.warning(message + ", caused by: " + cause.getMessage());
break;
case ERROR:
case FATAL:
log.severe(message + ", caused by: " + cause.getMessage());
this.hasSevereFailures = true;
break;
}

}

public boolean hasSevereFailures() {
return hasSevereFailures;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.ActivationOS;
import org.apache.maven.model.ActivationProperty;
import org.apache.maven.model.Profile;
import org.apache.maven.model.Repository;
import org.jboss.shrinkwrap.resolver.api.ResolutionException;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependency;
Expand Down Expand Up @@ -399,6 +404,107 @@ public static Proxy asProxy(org.apache.maven.settings.Proxy proxy) {
return aetherProxy;
}

public static Profile asProfile(org.apache.maven.settings.Profile profile) {
Profile mavenProfile = new Profile();

if (profile != null) {
mavenProfile.setId(profile.getId());
mavenProfile.setActivation(asActivation(profile.getActivation()));
mavenProfile.setProperties(profile.getProperties());
mavenProfile.setRepositories(asRepositories(profile.getRepositories()));
mavenProfile.setPluginRepositories(asRepositories(profile.getPluginRepositories()));
}

return mavenProfile;
}

public static List<Profile> asProfiles(List<org.apache.maven.settings.Profile> profiles) {
List<Profile> mavenProfiles = new ArrayList<Profile>();
for (org.apache.maven.settings.Profile p : profiles) {
mavenProfiles.add(asProfile(p));
}

return mavenProfiles;
}

private static Repository asRepository(org.apache.maven.settings.Repository repository) {
Repository mavenRepository = new Repository();
if (repository != null) {
mavenRepository.setId(repository.getId());
mavenRepository.setLayout(repository.getLayout());
mavenRepository.setName(repository.getName());
mavenRepository.setUrl(repository.getUrl());
mavenRepository.setReleases(asMavenRepositoryPolicy(repository.getReleases()));
mavenRepository.setSnapshots(asMavenRepositoryPolicy(repository.getSnapshots()));
}

return mavenRepository;
}

private static List<Repository> asRepositories(List<org.apache.maven.settings.Repository> repositories) {
List<Repository> mavenRepositories = new ArrayList<Repository>();
for (org.apache.maven.settings.Repository repository : repositories) {
mavenRepositories.add(asRepository(repository));
}

return mavenRepositories;
}

private static Activation asActivation(org.apache.maven.settings.Activation activation) {
Activation mavenActivation = new Activation();

if (activation != null) {
mavenActivation.setActiveByDefault(activation.isActiveByDefault());
mavenActivation.setJdk(activation.getJdk());
if (activation.getFile() != null) {
mavenActivation.setFile(asActivationFile(activation.getFile()));
}
if (activation.getOs() != null) {
mavenActivation.setOs(asActivationOS(activation.getOs()));
}
if (activation.getProperty() != null) {
mavenActivation.setProperty(asActivationProperty(activation.getProperty()));
}
}

return mavenActivation;
}

private static ActivationFile asActivationFile(org.apache.maven.settings.ActivationFile file) {
ActivationFile mavenActivationFile = new ActivationFile();

if (file != null) {
mavenActivationFile.setExists(file.getExists());
mavenActivationFile.setMissing(file.getMissing());
}

return mavenActivationFile;
}

private static ActivationOS asActivationOS(org.apache.maven.settings.ActivationOS os) {
ActivationOS mavenOS = new ActivationOS();

if (os != null) {
mavenOS.setArch(os.getArch());
mavenOS.setFamily(os.getFamily());
mavenOS.setName(os.getName());
mavenOS.setVersion(os.getVersion());
}

return mavenOS;
}

private static ActivationProperty asActivationProperty(org.apache.maven.settings.ActivationProperty property) {
ActivationProperty mavenProperty = new ActivationProperty();

if (property != null) {
mavenProperty.setName(property.getName());
mavenProperty.setValue(property.getValue());
}

return mavenProperty;
}

// converts repository policy
private static RepositoryPolicy asRepositoryPolicy(org.apache.maven.model.RepositoryPolicy policy) {
boolean enabled = true;
Expand Down Expand Up @@ -437,4 +543,18 @@ private static RepositoryPolicy asRepositoryPolicy(org.apache.maven.settings.Rep
return new RepositoryPolicy(enabled, updates, checksums);
}

// converts repository policy
private static org.apache.maven.model.RepositoryPolicy asMavenRepositoryPolicy(
org.apache.maven.settings.RepositoryPolicy policy) {

org.apache.maven.model.RepositoryPolicy mavenPolicy = new org.apache.maven.model.RepositoryPolicy();
if (policy != null) {
mavenPolicy.setChecksumPolicy(policy.getChecksumPolicy());
mavenPolicy.setUpdatePolicy(policy.getUpdatePolicy());
mavenPolicy.setEnabled(policy.isEnabled());
}

return mavenPolicy;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jboss.shrinkwrap.resolver.impl.maven;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
import org.jboss.shrinkwrap.resolver.api.ResolutionException;
Expand Down Expand Up @@ -41,14 +45,19 @@ public MavenDependencyResolver loadSettings(String userSettings) {
}

@Override
public EffectivePomMavenDependencyResolver loadEffectivePom(String path) throws ResolutionException {
public EffectivePomMavenDependencyResolver loadEffectivePom(String path, String... profiles) throws ResolutionException {

Validate.notNullOrEmpty(path, "Path to a POM file must be specified");
String resolvedPath = ResourceUtil.resolvePathByQualifier(path);
Validate.isReadable(resolvedPath, "Path to the pom.xml ('" + path + "')file must be defined and accessible");

File pom = new File(resolvedPath);
this.maven = maven.execute(new DefaultModelBuildingRequest().setPomFile(pom));
DefaultModelBuildingRequest request = new DefaultModelBuildingRequest()
.setSystemProperties(SecurityActions.getProperties()).setProfiles(maven.getSettingsDefinedProfiles())
.setPomFile(pom).setActiveProfileIds(explicitlyActivatedProfiles(profiles))
.setInactiveProfileIds(explicitlyDisabledProfiles(profiles));

this.maven = maven.execute(request);
return new EffectivePomMavenDependencyResolverImpl(maven);
}

Expand Down Expand Up @@ -85,4 +94,37 @@ public MavenEnvironment getMavenEnvironment() {
return maven;
}

// selects all profile ids to be activated
private static List<String> explicitlyActivatedProfiles(String... profiles) {
if (profiles.length == 0) {
return Collections.<String> emptyList();
}
List<String> activated = new ArrayList<String>();
for (String profileId : profiles) {
Validate.notNull(profileId, "Invalid name (\"" + profileId + "\") of a profile to be activated");
if (!(profileId.startsWith("-") || profileId.startsWith("!"))) {
activated.add(profileId);
}
}

return activated;
}

// selects all profiles ids to be disabled
private static List<String> explicitlyDisabledProfiles(String... profiles) {
if (profiles.length == 0) {
return Collections.<String> emptyList();
}
List<String> disabled = new ArrayList<String>();
for (String profileId : profiles) {
if (profileId != null && (profileId.startsWith("-") || profileId.startsWith("!"))) {
String disabledId = profileId.substring(1);
Validate.notNull(disabledId, "Invalid name (\"" + profileId + "\") of a profile do be disabled");
disabled.add(disabledId);
}
}

return disabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public File[] resolveAsFiles(String... coordinates) throws ResolutionException {
* @throws ResolutionException If artifact coordinates are wrong or if version cannot be determined.
*/
@Override
public EffectivePomMavenDependencyShortcut withPom(final String path) throws ResolutionException {
this.delegate = delegate.loadEffectivePom(path).up();
public EffectivePomMavenDependencyShortcut withPom(final String path, String... profiles) throws ResolutionException {
this.delegate = delegate.loadEffectivePom(path, profiles).up();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.Stack;

import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.settings.building.SettingsBuildingRequest;
import org.jboss.shrinkwrap.resolver.api.ResolutionException;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependency;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolutionFilter;
import org.sonatype.aether.artifact.ArtifactTypeRegistry;
Expand All @@ -30,10 +32,19 @@ interface MavenEnvironment {

Stack<MavenDependency> getDependencies();

List<RemoteRepository> getRemoteRepositories();
/**
* Returns a list of remote repositories enabled from Maven settings. If an effective pom was loaded, and it actually
* contains any repositories, these are added as well.
*
* @return List of currently active repositories
* @throws ResolutionException If repositories cannot be resolved
*/
List<RemoteRepository> getRemoteRepositories() throws ResolutionException;

Model getModel();

List<Profile> getSettingsDefinedProfiles();

/**
* Regenerates session environment to match latest update
*
Expand Down

0 comments on commit a0b4fae

Please sign in to comment.