1   package com.atlassian.maven.plugins.amps;
2   
3   import java.io.File;
4   import java.util.List;
5   
6   import org.apache.maven.artifact.Artifact;
7   import org.apache.maven.execution.MavenSession;
8   import org.apache.maven.execution.ReactorManager;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.plugin.MojoFailureException;
11  import org.apache.maven.project.MavenProject;
12  import org.apache.maven.project.MavenProjectBuilder;
13  import org.jfrog.maven.annomojo.annotations.MojoGoal;
14  import org.jfrog.maven.annomojo.annotations.MojoParameter;
15  import org.jfrog.maven.annomojo.annotations.MojoRequiresProject;
16  
17  import static java.util.Collections.singletonList;
18  
19  /**
20   * Run the webapp without a plugin project
21   */
22  @MojoGoal ("run-standalone")
23  @MojoRequiresProject (false)
24  public class RunStandaloneMojo extends AbstractProductHandlerMojo
25  {
26      private final String
27          GROUP_ID = "com.atlassian.amps",
28          ARTIFACT_ID = "standalone";
29  
30      @MojoParameter (expression = "${component.org.apache.maven.project.MavenProjectBuilder}", required = true, readonly = true)
31      private MavenProjectBuilder projectBuilder;
32  
33      @SuppressWarnings("unchecked")
34      protected void doExecute() throws MojoExecutionException, MojoFailureException
35      {
36          final String version = getPluginInformation().getVersion();
37          final Artifact artifact = artifactFactory.createProjectArtifact(GROUP_ID, ARTIFACT_ID, version);
38          try
39          {
40              // overall goal here is to create a new MavenContext / MavenGoals for the standalone project
41              final MavenContext oldContext = getMavenContext();
42  
43              // construct new project for new context
44              final MavenProject
45                  oldProject = oldContext.getProject(),
46                  newProject = projectBuilder.buildFromRepository(artifact, repositories, localRepository);
47  
48              // horrible hack #1: buildFromRepository() doesn't actually set the project's remote repositories
49              newProject.setRemoteArtifactRepositories(oldProject.getRemoteArtifactRepositories());
50              newProject.setPluginArtifactRepositories(oldProject.getPluginArtifactRepositories());
51  
52              // horrible hack #2: we need to modify the session to use the new project as its reactor
53              final List<MavenProject> newReactor = singletonList(newProject);
54              final MavenSession
55                  oldSession = oldContext.getSession(),
56                  newSession = new MavenSession(
57                      oldSession.getContainer(),
58                      oldSession.getSettings(),
59                      oldSession.getLocalRepository(),
60                      oldSession.getEventDispatcher(),
61                      new ReactorManager(newReactor),
62                      oldSession.getGoals(),
63                      oldSession.getExecutionRootDirectory(),
64                      oldSession.getExecutionProperties(),
65                      oldSession.getUserProperties(),
66                      oldSession.getStartTime()
67                  );
68  
69              // horrible hack #3: we need to create a base directory from scratch, and convince the project to like it
70              final String baseDir = System.getProperty("user.dir") + "/amps-standalone/";
71              newProject.setBasedir(new File(baseDir));
72              projectBuilder.calculateConcreteState(newProject, newSession.getProjectBuilderConfiguration());
73  
74              // finally, execute run goal against standalone project
75              final MavenContext newContext = new MavenContext(
76                  newProject,
77                  newReactor,
78                  newSession,
79                  oldContext.getPluginManager(),
80                  oldContext.getLog());
81              new MavenGoals(newContext).executeAmpsRecursively(version, "run");
82          }
83          catch (Exception e)
84          {
85              throw new MojoExecutionException(e.getMessage(), e);
86          }
87      }
88  }