Skip to content

Commit

Permalink
default configurations in dependency management
Browse files Browse the repository at this point in the history
  • Loading branch information
mepeisen committed Sep 17, 2012
1 parent dc3c920 commit 97b510b
Show file tree
Hide file tree
Showing 16 changed files with 567 additions and 69 deletions.
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Set;

import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.DebugConfigurationListener;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.aether.RepositorySystemSession;

/**
* The component lookup factory for components that are configured via pom.xml.
Expand Down Expand Up @@ -117,10 +119,10 @@ public <T> T lookup(Class<T> clazz, Xpp3Dom[] configuration, MavenSession sessio

configure(
configuration,
session.getCurrentProject(),
session == null ? null : session.getCurrentProject(),
result,
realm,
session);
session == null ? new MavenSession(null, (RepositorySystemSession) null, new DefaultMavenExecutionRequest(), null) : session);

return result;
}
Expand Down Expand Up @@ -148,10 +150,10 @@ public <T> T lookup(Class<T> clazz, String roleHint, Xpp3Dom[] configuration, Ma

configure(
configuration,
session.getCurrentProject(),
session == null ? null : session.getCurrentProject(),
result,
realm,
session);
session == null ? new MavenSession(null, (RepositorySystemSession) null, new DefaultMavenExecutionRequest(), null) : session);

return result;
}
Expand Down Expand Up @@ -328,6 +330,9 @@ private void populatePluginFields(Object component, PlexusConfiguration configur
*/
@Override
public Xpp3Dom getBuildConfig(final MavenProject project, String groupid, String artifactId) {
if (project == null) {
return null;
}
final List<Plugin> plugins = project.getBuildPlugins();
for (final Plugin plugin : plugins) {
if (plugin.getGroupId().equals(groupid)
Expand Down Expand Up @@ -394,10 +399,10 @@ public <T extends IService> T[] getServiceImplementations(Class<T> type,
for (final T res : result) {
configure(
config,
session.getCurrentProject(),
session == null ? null : session.getCurrentProject(),
res,
realm,
session);
session == null ? new MavenSession(null, (RepositorySystemSession) null, new DefaultMavenExecutionRequest(), null) : session);
}
return result;
}
Expand Down
Expand Up @@ -100,5 +100,11 @@ public interface IDependencyConfiguration {
* @return bootstrap php file.
*/
File getBootstrapFile();

/**
* Returns the default actions of this project
* @return default actions
*/
Iterable<IAction> getDefaults();

}
Expand Up @@ -22,6 +22,7 @@

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Configuration;
import org.phpmaven.dependency.IAction;
import org.phpmaven.dependency.IDependency;
import org.phpmaven.dependency.IDependencyConfiguration;

Expand All @@ -44,6 +45,12 @@ public class DependencyConfiguration implements IDependencyConfiguration {
* A bootstrap file for bootstrap dependency actions.
*/
private File bootstrapFile;

/**
* The default actions.
*/
@Configuration(name = "defaults", value = "")
private ArrayList<IAction> defaults = new ArrayList<IAction>();


/**
Expand All @@ -59,5 +66,11 @@ public Iterable<IDependency> getDependencies() {
public File getBootstrapFile() {
return this.bootstrapFile;
}


@Override
public Iterable<IAction> getDefaults() {
return this.defaults;
}

}
@@ -0,0 +1,156 @@
/**
* Copyright 2010-2012 by PHP-maven.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.phpmaven.project.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.phpmaven.core.IComponentFactory;
import org.phpmaven.dependency.IAction;
import org.phpmaven.dependency.IDependency;
import org.phpmaven.dependency.IDependencyConfiguration;
import org.phpmaven.dependency.IAction.ActionType;
import org.phpmaven.dependency.impl.Classic;
import org.phpmaven.exec.PhpException;

/**
* Helper to process and calculate the dependency actions on a dependency
*
* @author Martin Eisengardt <Martin.Eisengardt@googlemail.com>
* @since 2.0.3
*/
public class DependencyHelper {

/**
* Resolves the artifact.
* @param groupId group id
* @param artifactId artifact id
* @param version version
* @param type type
* @param classifier classifier
* @return the resolved artifact
* @throws PhpException thrown on resolve errors
*/
private static Artifact resolveArtifact(
String groupId, String artifactId, String version, String type, String classifier, RepositorySystem reposSystem,
MavenSession session)
throws MojoExecutionException {
final Artifact artifact = reposSystem.createArtifactWithClassifier(
groupId, artifactId, version, type, classifier);
final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
request.setArtifact(artifact);
request.setLocalRepository(session.getLocalRepository());
request.setOffline(session.isOffline());
final Set<ArtifactRepository> setRepos = new HashSet<ArtifactRepository>(
session.getRequest().getRemoteRepositories());
setRepos.addAll(session.getCurrentProject().getRemoteArtifactRepositories());
request.setRemoteRepositories(new ArrayList<ArtifactRepository>(setRepos));
final ArtifactResolutionResult result = reposSystem.resolve(request);
if (!result.isSuccess()) {
throw new MojoExecutionException("dependency resolution failed for " +
groupId + ":" + artifactId + ":" + version);
}

final Artifact resultArtifact = result.getArtifacts().iterator().next();
return resultArtifact;
}

public static Iterable<IAction> getDependencyActions(
Artifact dep, IDependencyConfiguration depConfig, RepositorySystem reposSystem, MavenSession session,
ProjectBuilder projectBuilder, IComponentFactory factory) throws MojoExecutionException {
final List<IAction> actions = new ArrayList<IAction>();
boolean hasClassic = true;

// try to find dependency configuration
for (final IDependency depCfg : depConfig.getDependencies()) {
if (depCfg.getGroupId().equals(dep.getGroupId()) &&
depCfg.getArtifactId().equals(dep.getArtifactId())) {
hasClassic = false;
for (final IAction action : depCfg.getActions()) {
if (action.getType() == ActionType.ACTION_CLASSIC) {
hasClassic = true;
} else {
actions.add(action);
}
}
break;
}
}

if (hasClassic) {
// try to resolve project defaults
final Artifact artifact = resolveArtifact(dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), "pom", null, reposSystem, session);
final File pomFile = artifact.getFile();
final ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setProcessPlugins(false);
ProjectBuildingResult pbres;
try {
pbres = projectBuilder.build(pomFile, pbr);
} catch (ProjectBuildingException e) {
throw new MojoExecutionException("Error building project", e);
}
final MavenProject project = pbres.getProject();
final Xpp3Dom pluginConfig = factory.getBuildConfig(project, "org.phpmaven", "maven-php-dependency");
if (pluginConfig != null) {
final Xpp3Dom defaultConfig = pluginConfig.getChild("defaults");
if (defaultConfig != null) {
// filter the other nodes to prevent from broken configs on future versions
while (pluginConfig.getChildCount() > 0) pluginConfig.removeChild(0);
pluginConfig.addChild(defaultConfig);
IDependencyConfiguration defCfg;
try {
defCfg = factory.lookup(IDependencyConfiguration.class, pluginConfig, null);
} catch (ComponentLookupException e) {
throw new MojoExecutionException("error receiving default config", e);
} catch (PlexusConfigurationException e) {
throw new MojoExecutionException("error receiving default config", e);
}
for (final IAction action : defCfg.getDefaults()) {
actions.add(action);
}
hasClassic = false;
}
}

// if hasClassic is still true we did not find any default config
if (hasClassic) {
actions.add(new Classic());
}
}
return actions;
}

}
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
Expand Down Expand Up @@ -102,6 +103,12 @@ public class PhpProject implements IPhpProject {
*/
@Requirement
private ProjectBuilder mavenProjectBuilder;

/**
* the repository system.
*/
@Requirement
private RepositorySystem reposSystem;

@Override
public void prepareDependencies(final Log log, File targetDir, String sourceScope)
Expand Down Expand Up @@ -129,45 +136,34 @@ public void prepareDependencies(final Log log, File targetDir, String sourceScop

final List<String> packedElements = new ArrayList<String>();
packedElements.add(dep.getFile().getAbsolutePath());
boolean isClassic = true;
for (final IDependency depCfg : depConfig.getDependencies()) {
if (depCfg.getGroupId().equals(dep.getGroupId()) &&
depCfg.getArtifactId().equals(dep.getArtifactId())) {
isClassic = false;
for (final IAction action : depCfg.getActions()) {
switch (action.getType()) {
case ACTION_BOOTSTRAP:
performBootstrap(log, info, dep, bootstrapInfo);
break;
case ACTION_CLASSIC:
isClassic = true;
break;
case ACTION_PEAR:
performPearInstall(log, dep, info);
break;
case ACTION_IGNORE:
// do nothing, isClassic should be false so that it is ignored
log.info(dep.getFile().getAbsolutePath() + " will be ignored");
break;
case ACTION_INCLUDE:
log.info(dep.getFile().getAbsolutePath() + " will be added on include path");
break;
case ACTION_EXTRACT:
performExtraction(log, dep, packedElements,
action, info);
break;
case ACTION_EXTRACT_INCLUDE:
performExtractAndInclude(log, dep,
packedElements, action, info);
break;
}
}
}
}

if (isClassic) {
performClassic(log, targetDir, dep, packedElements, info);
}
for (final IAction action : DependencyHelper.getDependencyActions(dep, depConfig, reposSystem, session, mavenProjectBuilder, factory)) {
switch (action.getType()) {
case ACTION_BOOTSTRAP:
performBootstrap(log, info, dep, bootstrapInfo);
break;
case ACTION_CLASSIC:
performClassic(log, targetDir, dep, packedElements, info);
break;
case ACTION_PEAR:
performPearInstall(log, dep, info);
break;
case ACTION_IGNORE:
// do nothing, isClassic should be false so that it is ignored
log.info(dep.getFile().getAbsolutePath() + " will be ignored");
break;
case ACTION_INCLUDE:
log.info(dep.getFile().getAbsolutePath() + " will be added on include path");
break;
case ACTION_EXTRACT:
performExtraction(log, dep, packedElements,
action, info);
break;
case ACTION_EXTRACT_INCLUDE:
performExtractAndInclude(log, dep,
packedElements, action, info);
break;
}
}
}
} catch (IOException ex) {
throw new MojoExecutionException("Failed preparing dependencies", ex);
Expand Down

0 comments on commit 97b510b

Please sign in to comment.