use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.
the class InstallFileMojoTest method createMavenSession.
private MavenSession createMavenSession() {
MavenSession session = mock(MavenSession.class);
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManager(new File(LOCAL_REPO)));
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession(repositorySession);
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
return session;
}
use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.
the class AbstractHelpMojo method getMavenProject.
/**
* Retrieves the Maven Project associated with the given artifact String, in the form of
* <code>groupId:artifactId[:version]</code>. This resolves the POM artifact at those coordinates and then builds
* the Maven project from it.
*
* @param artifactString Coordinates of the Maven project to get.
* @return New Maven project.
* @throws MojoExecutionException If there was an error while getting the Maven project.
*/
protected MavenProject getMavenProject(String artifactString) throws MojoExecutionException {
ArtifactCoordinate coordinate = getArtifactCoordinate(artifactString, "pom");
try {
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setRemoteRepositories(remoteRepositories);
pbr.setProject(null);
pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
pbr.setResolveDependencies(true);
Artifact artifact = artifactResolver.resolveArtifact(pbr, coordinate).getArtifact();
return projectBuilder.build(artifact.getFile(), pbr).getProject();
} catch (Exception e) {
throw new MojoExecutionException("Unable to get the POM for the artifact '" + artifactString + "'. Verify the artifact parameter.", e);
}
}
use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.
the class AbstractJavadocMojo method getPathElements.
/**
* Method that gets the classpath and modulepath elements that will be specified in the javadoc
* <code>-classpath</code> and <code>--module-path</code> parameter.
* Since we have all the sources of the current reactor, it is sufficient to consider the
* dependencies of the reactor modules, excluding the module artifacts which may not yet be available
* when the reactor project is built for the first time.
*
* @return all classpath elements
* @throws MavenReportException if any.
*/
private List<File> getPathElements() throws MavenReportException {
List<File> classpathElements = new ArrayList<>();
Map<String, Artifact> compileArtifactMap = new HashMap<>();
if (isTest()) {
classpathElements.addAll(getProjectBuildOutputDirs(project));
}
populateCompileArtifactMap(compileArtifactMap, project.getArtifacts());
if (isAggregator() && project.isExecutionRoot()) {
List<String> reactorArtifacts = new ArrayList<>();
for (MavenProject p : reactorProjects) {
reactorArtifacts.add(p.getGroupId() + ':' + p.getArtifactId());
}
TransformableFilter dependencyFilter = new AndFilter(Arrays.asList(new PatternExclusionsFilter(reactorArtifacts), getDependencyScopeFilter()));
for (MavenProject subProject : reactorProjects) {
if (subProject != project) {
classpathElements.addAll(getProjectBuildOutputDirs(subProject));
try {
StringBuilder sb = new StringBuilder();
sb.append("Compiled artifacts for ");
sb.append(subProject.getGroupId()).append(":");
sb.append(subProject.getArtifactId()).append(":");
sb.append(subProject.getVersion()).append('\n');
ProjectBuildingRequest buildingRequest = session.getProjectBuildingRequest();
buildingRequest = buildingRequest.setRemoteRepositories(subProject.getRemoteArtifactRepositories());
for (ArtifactResult artifactResult : dependencyResolver.resolveDependencies(buildingRequest, subProject.getDependencies(), null, dependencyFilter)) {
populateCompileArtifactMap(compileArtifactMap, Collections.singletonList(artifactResult.getArtifact()));
sb.append(artifactResult.getArtifact().getFile()).append('\n');
}
if (getLog().isDebugEnabled()) {
getLog().debug(sb.toString());
}
} catch (DependencyResolverException e) {
throw new MavenReportException(e.getMessage(), e);
}
}
}
}
for (Artifact a : compileArtifactMap.values()) {
classpathElements.add(a.getFile());
}
if (additionalDependencies != null) {
for (Dependency dependency : additionalDependencies) {
Artifact artifact = resolveDependency(dependency);
getLog().debug("add additional artifact with path " + artifact.getFile());
classpathElements.add(artifact.getFile());
}
}
return classpathElements;
}
use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.
the class JavadocReportTest method testTaglets.
/**
* Method to test the taglet artifact configuration
*
* @throws Exception if any
*/
public void testTaglets() throws Exception {
// ----------------------------------------------------------------------
// taglet-test: check if a taglet is used
// ----------------------------------------------------------------------
File testPom = new File(unit, "taglet-test/taglet-test-plugin-config.xml");
JavadocReport mojo = lookupMojo(testPom);
MavenSession session = spy(newMavenSession(mojo.project));
ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
when(session.getRepositorySession()).thenReturn(repositorySession);
LegacySupport legacySupport = lookup(LegacySupport.class);
legacySupport.setSession(session);
setVariableValueToObject(mojo, "session", session);
mojo.execute();
File apidocs = new File(getBasedir(), "target/test/unit/taglet-test/target/site/apidocs");
assertTrue(new File(apidocs, "index.html").exists());
File appFile = new File(apidocs, "taglet/test/App.html");
assertTrue(appFile.exists());
String appString = readFile(appFile);
assertTrue(appString.contains("<b>To Do:</b>"));
}
use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.
the class JavadocReportTest method testStylesheetfile.
/**
* Method to test the <code><stylesheetfile/></code> parameter.
*
* @throws Exception if any
*/
public void testStylesheetfile() throws Exception {
File testPom = new File(unit, "stylesheetfile-test/pom.xml");
JavadocReport mojo = lookupMojo(testPom);
assertNotNull(mojo);
MavenSession session = spy(newMavenSession(mojo.project));
ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
when(session.getRepositorySession()).thenReturn(repositorySession);
LegacySupport legacySupport = lookup(LegacySupport.class);
legacySupport.setSession(session);
setVariableValueToObject(mojo, "session", session);
File apidocs = new File(getBasedir(), "target/test/unit/stylesheetfile-test/target/site/apidocs");
File stylesheetfile = new File(apidocs, "stylesheet.css");
File options = new File(apidocs, "options");
// stylesheet == maven OR java
setVariableValueToObject(mojo, "stylesheet", "javamaven");
try {
mojo.execute();
assertTrue(false);
} catch (Exception e) {
assertTrue(true);
}
// stylesheet == java
setVariableValueToObject(mojo, "stylesheet", "java");
mojo.execute();
String content = readFile(stylesheetfile);
assertTrue(content.contains("/* Javadoc style sheet */"));
String optionsContent = readFile(options);
assertFalse(optionsContent.contains("-stylesheetfile"));
// stylesheet == maven
setVariableValueToObject(mojo, "stylesheet", "maven");
mojo.execute();
content = readFile(stylesheetfile);
assertTrue(content.contains("/* Javadoc style sheet */") && content.contains("Licensed to the Apache Software Foundation (ASF) under one"));
optionsContent = readFile(options);
assertTrue(optionsContent.contains("-stylesheetfile"));
assertTrue(optionsContent.contains("'" + stylesheetfile.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
// stylesheetfile defined as a project resource
setVariableValueToObject(mojo, "stylesheet", null);
setVariableValueToObject(mojo, "stylesheetfile", "com/mycompany/app/javadoc/css/stylesheet.css");
mojo.execute();
content = readFile(stylesheetfile);
assertTrue(content.contains("/* Custom Javadoc style sheet in project */"));
optionsContent = readFile(options);
assertTrue(optionsContent.contains("-stylesheetfile"));
File stylesheetResource = new File(unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css/stylesheet.css");
assertTrue(optionsContent.contains("'" + stylesheetResource.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
// stylesheetfile defined in a javadoc plugin dependency
setVariableValueToObject(mojo, "stylesheetfile", "com/mycompany/app/javadoc/css2/stylesheet.css");
mojo.execute();
content = readFile(stylesheetfile);
assertTrue(content.contains("/* Custom Javadoc style sheet in artefact */"));
optionsContent = readFile(options);
assertTrue(optionsContent.contains("-stylesheetfile"));
assertTrue(optionsContent.contains("'" + stylesheetfile.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
// stylesheetfile defined as file
File css = new File(unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css");
setVariableValueToObject(mojo, "stylesheetfile", css.getAbsolutePath());
mojo.execute();
content = readFile(stylesheetfile);
assertTrue(content.contains("/* Custom Javadoc style sheet as file */"));
optionsContent = readFile(options);
assertTrue(optionsContent.contains("-stylesheetfile"));
stylesheetResource = new File(unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css");
assertTrue(optionsContent.contains("'" + stylesheetResource.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
}
Aggregations