Java Code Examples for org.apache.maven.execution.MavenSession#setProjects()

The following examples show how to use org.apache.maven.execution.MavenSession#setProjects() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstallDeployTest.java    From takari-lifecycle with Eclipse Public License 1.0 6 votes vote down vote up
private MavenSession newSession(MavenProject project, File localrepo, Properties properties) throws Exception {
  MavenExecutionRequest request = new DefaultMavenExecutionRequest();
  MavenExecutionResult result = new DefaultMavenExecutionResult();
  DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
  LocalRepository localRepo = new LocalRepository(localrepo);
  repoSession.setLocalRepositoryManager(mojos.getContainer().lookup(LocalRepositoryManagerFactory.class, "simple").newInstance(repoSession, localRepo));
  MavenSession session = new MavenSession(mojos.getContainer(), repoSession, request, result);
  List<MavenProject> projects = new ArrayList<>();
  projects.add(project);
  for (String module : project.getModules()) {
    MavenProject moduleProject = readMavenProject(new File(project.getBasedir(), module), properties);
    moduleProject.setParent(project);
    projects.add(moduleProject);
  }

  session.setProjects(projects);
  return session;
}
 
Example 2
Source File: PluginDescriptorMojoTest.java    From takari-lifecycle with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testSourcepathDependency() throws Exception {
  // assert that MojoAnnotationProcessorMojo honours sourcepath=reactorDependencies

  File basedir = resources.getBasedir("plugin-descriptor/sourcepath-dependency");
  File dependency = new File(basedir, "dependency");
  File plugin = new File(basedir, "plugin");

  MavenProject dependencyProject = mojos.readMavenProject(dependency);
  MavenProject pluginProject = mojos.readMavenProject(plugin);
  addDependencies(pluginProject, "apache-plugin-annotations-jar", "maven-plugin-api-jar");
  mojos.newDependency(new File(dependencyProject.getBuild().getOutputDirectory())) //
      .setGroupId(dependencyProject.getGroupId()) //
      .setArtifactId(dependencyProject.getArtifactId()) //
      .setVersion(dependencyProject.getVersion()) //
      .addTo(pluginProject);
  Artifact dependencyArtifact = dependencyProject.getArtifact();
  dependencyArtifact.setScope(Artifact.SCOPE_COMPILE);
  pluginProject.getArtifacts().add(dependencyArtifact);
  MavenSession session = mojos.newMavenSession(pluginProject);
  session.setProjects(ImmutableList.of(dependencyProject, pluginProject));
  session.setCurrentProject(pluginProject);
  mojos.executeMojo(session, pluginProject, "mojo-annotation-processor", newParameter("sourcepath", "reactorDependencies"));
  mojos.executeMojo(session, pluginProject, "plugin-descriptor");
  mojos.assertBuildOutputs(plugin, "target/classes/META-INF/maven/plugin.xml", "target/classes/META-INF/m2e/lifecycle-mapping-metadata.xml");
}
 
Example 3
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 6 votes vote down vote up
@Test
@Ignore("Neither javac nor jdt support secondary types on sourcepath")
public void testSourcepathSecondatytype() throws Exception {
  File processor = compileAnnotationProcessor();
  File basedir = resources.getBasedir("compile-proc/proc-sourcepath-secondarytype");

  File dependencyBasedir = new File(basedir, "dependency");
  File projectBasedir = new File(basedir, "project");

  Xpp3Dom processors = newProcessors("processor.Processor");
  Xpp3Dom sourcepath = newParameter("sourcepath", "reactorDependencies");

  MavenProject dependency = mojos.readMavenProject(dependencyBasedir);
  MavenProject project = mojos.readMavenProject(projectBasedir);

  mojos.newDependency(new File(dependencyBasedir, "target/classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setVersion(dependency.getVersion()) //
      .addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  session.setProjects(Arrays.asList(project, dependency));

  processAnnotations(session, project, "compile", processor, Proc.only, processors, sourcepath);
}
 
Example 4
Source File: BetterMojoRule.java    From swaggerhub-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Extends the super to use the new {@link #newMavenSession()} introduced here
 * which sets the defaults one expects from maven; the standard test case leaves a lot of things blank
 */
@Override
public MavenSession newMavenSession(MavenProject project) {
    MavenSession session = newMavenSession();
    session.setCurrentProject(project);
    session.setProjects(Arrays.asList(project));
    return session;
}
 
Example 5
Source File: BetterAbstractMojoTestCase.java    From repairnator with MIT License 5 votes vote down vote up
/** Extends the super to use the new {@link #newMavenSession()} introduced here 
 * which sets the defaults one expects from maven; the standard test case leaves a lot of things blank */
@Override
protected MavenSession newMavenSession(MavenProject project) {
    MavenSession session = newMavenSession();
    session.setCurrentProject( project );
    session.setProjects( Arrays.asList( project ) );
    return session;        
}
 
Example 6
Source File: GenerateBomMojo.java    From sundrio with Apache License 2.0 5 votes vote down vote up
private void build(MavenSession session, MavenProject project, List<MavenProject> allProjects, GoalSet goals) throws MojoExecutionException {
    session.setProjects(allProjects);
    ProjectIndex projectIndex = new ProjectIndex(session.getProjects());
    try {
        ReactorBuildStatus reactorBuildStatus = new ReactorBuildStatus(new BomDependencyGraph(session.getProjects()));
        ReactorContext reactorContext = new ReactorContextFactory(new MavenVersion(mavenVersion)).create(session.getResult(), projectIndex, Thread.currentThread().getContextClassLoader(),
                reactorBuildStatus, builder);
        List<TaskSegment> segments = segmentCalculator.calculateTaskSegments(session);
        for (TaskSegment segment : segments) {
            builder.buildProject(session, reactorContext, project, filterSegment(segment, goals));
        }
    } catch (Throwable t) {
        throw new MojoExecutionException("Error building generated bom:" + project.getArtifactId(), t);
    }
}
 
Example 7
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testRequireProc_processorpath() throws Exception {
  MavenProject annotations = mojos.readMavenProject(resources.getBasedir("compile-proc/processorpath-annotation"));
  mojos.compile(annotations);

  MavenProject processor = mojos.readMavenProject(resources.getBasedir("compile-proc/processorpath-processor"));
  mojos.newDependency(new File(annotations.getBasedir(), "target/classes")).setArtifactId("annotations").addTo(processor);
  mojos.compile(processor);
  cp(processor.getBasedir(), "src/main/resources/META-INF/services/javax.annotation.processing.Processor", "target/classes/META-INF/services/javax.annotation.processing.Processor");

  File basedir = resources.getBasedir("compile-proc/proc");
  MavenProject project = mojos.readMavenProject(basedir);
  mojos.newDependency(new File(annotations.getBasedir(), "target/classes")).setArtifactId("annotations").addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  SimpleReactorReader.builder() //
      .addProjects(annotations, processor, project) //
      .toSession(session.getRepositorySession());

  session.setProjects(Arrays.asList(annotations, processor, project));
  session.setCurrentProject(project);
  Xpp3Dom processorpath = new Xpp3Dom("processorpath");
  processorpath.addChild(newProcessorpathEntry(processor));
  try {
    mojos.compile(session, project, processorpath);
    fail();
  } catch (IllegalArgumentException expected) {
    // TODO assert message
  }
}
 
Example 8
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testProc_processorpath() throws Exception {
  MavenProject annotations = mojos.readMavenProject(resources.getBasedir("compile-proc/processorpath-annotation"));
  mojos.compile(annotations);

  MavenProject processor = mojos.readMavenProject(resources.getBasedir("compile-proc/processorpath-processor"));
  mojos.newDependency(new File(annotations.getBasedir(), "target/classes")).setArtifactId("annotations").addTo(processor);
  mojos.compile(processor);
  cp(processor.getBasedir(), "src/main/resources/META-INF/services/javax.annotation.processing.Processor", "target/classes/META-INF/services/javax.annotation.processing.Processor");

  File basedir = resources.getBasedir("compile-proc/proc");
  MavenProject project = mojos.readMavenProject(basedir);
  mojos.newDependency(new File(annotations.getBasedir(), "target/classes")).setArtifactId("annotations").addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  SimpleReactorReader.builder() //
      .addProjects(annotations, processor, project) //
      .toSession(session.getRepositorySession());

  session.setProjects(Arrays.asList(annotations, processor, project));
  session.setCurrentProject(project);
  Xpp3Dom processorpath = new Xpp3Dom("processorpath");
  processorpath.addChild(newProcessorpathEntry(processor));
  mojos.compile(session, project, newParameter("proc", "proc"), processorpath);
  mojos.assertBuildOutputs(new File(basedir, "target"), //
      "classes/proc/Source.class", //
      "generated-sources/annotations/proc/GeneratedSource.java", //
      "classes/proc/GeneratedSource.class");
}
 
Example 9
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testSourcepathDependency() throws Exception {
  File processor = compileAnnotationProcessor();
  File basedir = resources.getBasedir("compile-proc/proc-sourcepath");

  File dependencyBasedir = new File(basedir, "dependency");
  File projectBasedir = new File(basedir, "project");

  Xpp3Dom processors = newProcessors("processor.Processor");
  Xpp3Dom sourcepath = newParameter("sourcepath", "reactorDependencies");

  MavenProject dependency = mojos.readMavenProject(dependencyBasedir);
  MavenProject project = mojos.readMavenProject(projectBasedir);

  mojos.newDependency(new File(dependencyBasedir, "target/classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setVersion(dependency.getVersion()) //
      .addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  session.setProjects(Arrays.asList(project, dependency));

  processAnnotations(session, project, "compile", processor, Proc.only, processors, sourcepath);
  mojos.assertBuildOutputs(new File(projectBasedir, "target"), //
      "generated-sources/annotations/sourcepath/project/GeneratedSource.java" //
  );
}
 
Example 10
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testSourcepathDependencyNestedTypeThroughBinaryDependency() throws Exception {
  File processor = compileAnnotationProcessor();
  File basedir = resources.getBasedir("compile-proc/proc-sourcepath-nestedtype-through-binary");

  File dependencyBasedir = new File(basedir, "dependency");
  File binaryDependencyBasedir = new File(basedir, "binary-dependency");
  File projectBasedir = new File(basedir, "project");

  Xpp3Dom processors = newProcessors("processor.EnclosedElementsProcessor");
  Xpp3Dom sourcepath = newParameter("sourcepath", "reactorDependencies");

  MavenProject dependency = mojos.readMavenProject(dependencyBasedir);
  MavenProject project = mojos.readMavenProject(projectBasedir);

  mojos.newDependency(new File(binaryDependencyBasedir, "target/binary-dependency-1.0.jar")).addTo(project);

  mojos.newDependency(new File(dependencyBasedir, "target/classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setVersion(dependency.getVersion()) //
      .addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  session.setProjects(Arrays.asList(project, dependency));

  processAnnotations(session, project, "compile", processor, Proc.only, processors, sourcepath);
  mojos.assertBuildOutputs(new File(projectBasedir, "target"), //
      "generated-sources/annotations/sourcepath/nestedtype/project/GeneratedSource.java" //
  );
}
 
Example 11
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testSourcepathDependency_classifiedDependency() throws Exception {
  File processor = compileAnnotationProcessor();
  File basedir = resources.getBasedir("compile-proc/proc-sourcepath");

  File dependencyBasedir = new File(basedir, "dependency");
  File projectBasedir = new File(basedir, "project");

  Xpp3Dom processors = newProcessors("processor.Processor");
  Xpp3Dom sourcepath = newParameter("sourcepath", "reactorDependencies");

  MavenProject dependency = mojos.readMavenProject(dependencyBasedir);
  MavenProject project = mojos.readMavenProject(projectBasedir);

  mojos.newDependency(new File(dependencyBasedir, "target/classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setVersion(dependency.getVersion()) //
      .setClassifier("classifier") //
      .addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  session.setProjects(Arrays.asList(project, dependency));

  try {
    processAnnotations(session, project, "compile", processor, Proc.only, processors, sourcepath);
    Assert.fail();
  } catch (MojoExecutionException expected) {
    Assert.assertTrue(expected.getMessage().contains(dependency.getGroupId() + ":" + dependency.getArtifactId()));
  }
}
 
Example 12
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testSourcepathDependency_testCompile() throws Exception {
  File processor = compileAnnotationProcessor();
  File basedir = resources.getBasedir("compile-proc/proc-sourcepath");

  File dependencyBasedir = new File(basedir, "dependency");
  File projectBasedir = new File(basedir, "project");

  Xpp3Dom processors = newProcessors("processor.Processor");
  Xpp3Dom sourcepath = newParameter("sourcepath", "reactorDependencies");

  MavenProject dependency = mojos.readMavenProject(dependencyBasedir);
  MavenProject project = mojos.readMavenProject(projectBasedir);

  mojos.newDependency(new File(dependencyBasedir, "target/classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setVersion(dependency.getVersion()) //
      .addTo(project);

  mojos.newDependency(new File(dependencyBasedir, "target/test-classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setType("test-jar") //
      .setVersion(dependency.getVersion()) //
      .addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  session.setProjects(Arrays.asList(project, dependency));

  processAnnotations(session, project, "testCompile", processor, Proc.only, processors, sourcepath);
  mojos.assertBuildOutputs(new File(projectBasedir, "target"), //
      "generated-test-sources/test-annotations/sourcepath/project/test/GeneratedSourceTest.java" //
  );
}
 
Example 13
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testSourcepathDependency_incremental() throws Exception {
  // the point of this test is assert that changes to sourcepath files are expected to trigger reprocessing of affected sources

  File processor = compileAnnotationProcessor();
  File basedir = resources.getBasedir("compile-proc/proc-sourcepath");

  File dependencyBasedir = new File(basedir, "dependency");
  File projectBasedir = new File(basedir, "project");

  Xpp3Dom processors = newProcessors("processor.Processor");
  Xpp3Dom sourcepath = newParameter("sourcepath", "reactorDependencies");

  MavenProject dependency = mojos.readMavenProject(dependencyBasedir);
  MavenProject project = mojos.readMavenProject(projectBasedir);

  mojos.newDependency(new File(dependencyBasedir, "target/classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setVersion(dependency.getVersion()) //
      .addTo(project);

  MavenSession session = mojos.newMavenSession(project);
  session.setProjects(Arrays.asList(project, dependency));

  processAnnotations(session, project, "compile", processor, Proc.only, processors, sourcepath);
  mojos.assertBuildOutputs(new File(projectBasedir, "target"), //
      "generated-sources/annotations/sourcepath/project/GeneratedSource.java" //
  );

  // second, incremental, compilation with one of sourcepath files removed

  rm(dependencyBasedir, "src/main/java/sourcepath/dependency/SourcepathDependency.java");
  mojos.flushClasspathCaches();

  dependency = mojos.readMavenProject(dependencyBasedir);
  project = mojos.readMavenProject(projectBasedir);

  mojos.newDependency(new File(dependencyBasedir, "target/classes")) //
      .setGroupId(dependency.getGroupId()) //
      .setArtifactId(dependency.getArtifactId()) //
      .setVersion(dependency.getVersion()) //
      .addTo(project);

  session = mojos.newMavenSession(project);
  session.setProjects(Arrays.asList(project, dependency));
  try {
    processAnnotations(session, project, "compile", processor, Proc.only, processors, sourcepath);
  } catch (MojoExecutionException expected) {}
  ErrorMessage message = new ErrorMessage(compilerId);
  message.setSnippets(CompilerJdt.ID, "sourcepath.dependency.SourcepathDependency cannot be resolved to a type");
  message.setSnippets(CompilerJavac.ID, "package sourcepath.dependency does not exist");
  mojos.assertMessage(projectBasedir, "src/main/java/sourcepath/project/Source.java", message);
  // oddly enough, both jdt and javac generate GeneratedSource despite the error
}
 
Example 14
Source File: AnnotationProcessingTest.java    From takari-lifecycle with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testSourcepathCache() throws Exception {
  // project-a has its generated sources directory configured as compile source root
  // project-b depends on project-a generated sources
  // both project-a and project-b run proc=only sourcepath=reactorDependencies

  File processor = compileAnnotationProcessor();
  File basedir = resources.getBasedir("compile-proc/proc-sourcepath-cache");

  Xpp3Dom processors = newProcessors("processor.Processor");
  Xpp3Dom sourcepath = newParameter("sourcepath", "reactorDependencies");
  Xpp3Dom procOnly = newParameter("proc", Proc.only.name());

  // setting up project-a
  File projectaBasedir = new File(basedir, "project-a");
  MavenProject projecta = mojos.readMavenProject(projectaBasedir);
  addDependency(projecta, "processor", new File(processor, "target/classes"));
  projecta.addCompileSourceRoot(new File(projectaBasedir, "target/generated-sources/annotations").getCanonicalPath());

  // setting up project-b
  File projectbBasedir = new File(basedir, "project-b");
  MavenProject projectb = mojos.readMavenProject(projectbBasedir);
  addDependency(projectb, "processor", new File(processor, "target/classes"));
  mojos.newDependency(new File(projectaBasedir, "target/classes")) //
      .setGroupId(projecta.getGroupId()) //
      .setArtifactId(projecta.getArtifactId()) //
      .setVersion(projecta.getVersion()) //
      .addTo(projectb);

  MavenSession session = mojos.newMavenSession(projecta);
  session.setProjects(Arrays.asList(projecta, projecta));

  // process annotations in project-a
  mojos.executeMojo(session, projecta, "compile", procOnly, processors, sourcepath);
  mojos.assertBuildOutputs(new File(projectaBasedir, "target"), //
      "generated-sources/annotations/sourcepath/projecta/GeneratedSourceA.java" //
  );

  // process annotations in project-b, requires access to project-a generated sources
  session.setCurrentProject(projectb);
  mojos.executeMojo(session, projectb, "compile", procOnly, processors, sourcepath);
  mojos.assertBuildOutputs(new File(projectbBasedir, "target"), //
      "generated-sources/annotations/sourcepath/projectb/GeneratedSourceB.java" //
  );
}