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

The following examples show how to use org.apache.maven.execution.MavenSession#setCurrentProject() . 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: 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 2
Source File: CreateProjectMojo.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void createMavenWrapper(File createdPomFile, Properties props) {
    try {
        // we need to modify the maven environment used by the wrapper plugin since the project could have been
        // created in a directory other than the current
        MavenProject newProject = projectBuilder.build(
                createdPomFile, new DefaultProjectBuildingRequest(session.getProjectBuildingRequest())).getProject();

        MavenExecutionRequest newExecutionRequest = DefaultMavenExecutionRequest.copy(session.getRequest());
        newExecutionRequest.setBaseDirectory(createdPomFile.getParentFile());

        MavenSession newSession = new MavenSession(session.getContainer(), session.getRepositorySession(),
                newExecutionRequest, session.getResult());
        newSession.setCurrentProject(newProject);

        setProxySystemPropertiesFromSession();

        executeMojo(
                plugin(
                        groupId("io.takari"),
                        artifactId("maven"),
                        version(ToolsUtils.getMavenWrapperVersion(props))),
                goal("wrapper"),
                configuration(
                        element(name("maven"), ToolsUtils.getProposedMavenVersion(props))),
                executionEnvironment(
                        newProject,
                        newSession,
                        pluginManager));
    } catch (Exception e) {
        // no reason to fail if the wrapper could not be created
        getLog().error("Unable to install the Maven wrapper (./mvnw) in the project", e);
    }
}
 
Example 3
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 4
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 5
Source File: InstallDeployTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testDeployAtEnd() throws Exception {
  File basedir = resources.getBasedir("install-deploy/deploy-at-end");

  File localrepo = new File(basedir, "target/localrepo");
  Assert.assertTrue(localrepo.mkdirs());

  File remoterepo = new File(basedir, "target/remoterepo");
  Assert.assertTrue(remoterepo.mkdirs());

  Properties properties = new Properties();
  properties.put("version", "1.0");
  properties.put("repopath", remoterepo.getCanonicalPath());
  MavenProject project = readMavenProject(basedir, properties);

  MavenSession session = newSession(project, localrepo, properties);

  DeployParticipant deployParticipant = mojos.getContainer().lookup(DeployParticipant.class);

  Assert.assertEquals(0, deployParticipant.getDeployAtEndRequests().size());

  for (MavenProject reactorProject : session.getProjects()) {
    session.setCurrentProject(reactorProject);
    if (!reactorProject.getPackaging().equals("pom")) {
      mojos.executeMojo(session, reactorProject, "compile");
      mojos.executeMojo(session, reactorProject, "jar");
    }
    mojos.executeMojo(session, reactorProject, "deploy");
  }

  Assert.assertFalse(new File(remoterepo, "io/takari/lifecycle/its/test/1.0/test-1.0.pom").canRead());
  Assert.assertFalse(new File(remoterepo, "io/takari/lifecycle/its/test-modulea/1.0/test-modulea-1.0.jar").canRead());
  Assert.assertFalse(new File(remoterepo, "io/takari/lifecycle/its/test-modulea/1.0/test-modulea-1.0.pom").canRead());
  Assert.assertFalse(new File(remoterepo, "io/takari/lifecycle/its/test-moduleb/1.0/test-moduleb-1.0.jar").canRead());
  Assert.assertFalse(new File(remoterepo, "io/takari/lifecycle/its/test-moduleb/1.0/test-moduleb-1.0.pom").canRead());

  Assert.assertEquals(3, deployParticipant.getDeployAtEndRequests().size());
}
 
Example 6
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 7
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 8
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" //
  );
}