Details
Description
I get nullpointers all over the place if I do not set up the configuration options exactly as maven requires them. This is fine, but not a very friendly experience for developers trying to figure out how to use the API.
This is a correct/simple bootstrap (see below,) but if any or many of the methods are not called "just so" a nullpointer will result. Better error messages should be provided, such as:
"No LocalRepositoryManager was configured in the RespositorySession - this may not be null."
Etc...
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.arqmvn;
import java.io.File;
import java.util.ArrayList;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.project.DefaultProjectBuildingRequest;
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.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
import org.sonatype.aether.util.DefaultRepositorySystemSession;
/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class MavenFacet
{
private final ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
private DefaultPlexusContainer container = null;
private ProjectBuilder builder = null;
private void bootstrapMaven()
{
if (!initialized())
{
try
{
container = new DefaultPlexusContainer();
ConsoleLoggerManager loggerManager = new ConsoleLoggerManager();
loggerManager.setThreshold("ERROR");
container.setLoggerManager(loggerManager);
builder = container.lookup(ProjectBuilder.class);
// TODO this needs to be configurable via the project/.sidekick file.
String localRepository = getUserHomeDir().getAbsolutePath() + "/.m2/repository";
request.setLocalRepository(new MavenArtifactRepository(
"local", new File(localRepository).toURI().toURL().toString(),
container.lookup(ArtifactRepositoryLayout.class),
new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN),
new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN)));
request.setRemoteRepositories(new ArrayList<ArtifactRepository>());
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository));
repositorySession.setOffline(true);
request.setRepositorySession(repositorySession);
request.setProcessPlugins(true);
request.setResolveDependencies(true);
// request.setOffline(true);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
private boolean initialized()
{
return request == null;
}
/*
* POM manipulation methods
*/
public ProjectBuildingResult getProjectBuildingResult()
{
bootstrapMaven();
try
{
ProjectBuildingResult buildingResult = builder.build(getPOMFile(), request);
return buildingResult;
}
catch (ProjectBuildingException e)
{
throw new RuntimeException(e);
}
}
private File getPOMFile()
{
File file = new File("pom.xml");
return file;
}
private File getUserHomeDir()
{
return new File(System.getProperty("user.home")).getAbsoluteFile();
}
}