1 package com.atlassian.maven.plugins.amps;
2
3 import com.atlassian.maven.plugins.amps.product.ProductHandler;
4 import org.apache.commons.io.IOUtils;
5 import org.apache.maven.plugin.MojoExecutionException;
6 import org.apache.maven.plugin.MojoFailureException;
7 import org.apache.maven.artifact.Artifact;
8 import org.jfrog.maven.annomojo.annotations.MojoExecute;
9 import org.jfrog.maven.annomojo.annotations.MojoGoal;
10 import org.jfrog.maven.annomojo.annotations.MojoParameter;
11 import org.jfrog.maven.annomojo.annotations.MojoRequiresDependencyResolution;
12
13 import java.io.File;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Properties;
20
21 import static org.apache.commons.lang.StringUtils.isBlank;
22
23
24
25
26 @MojoGoal ("run")
27 @MojoExecute (phase = "package")
28 @MojoRequiresDependencyResolution
29 public class RunMojo extends AbstractProductHandlerMojo
30 {
31 private static final char CONTROL_C = (char) 27;
32
33 @MojoParameter (expression = "${wait}", defaultValue = "true")
34 private boolean wait;
35
36
37
38
39 @MojoParameter (expression = "${amps.properties}", required = true, defaultValue = "false")
40 protected boolean writePropertiesToFile;
41
42
43
44
45 @MojoParameter(expression = "${instanceId}")
46 protected String instanceId;
47
48
49
50
51 protected final Map<String, String> properties = new HashMap<String, String>();
52
53 protected void doExecute() throws MojoExecutionException, MojoFailureException
54 {
55 final MavenGoals goals = getMavenGoals();
56 Product ctx;
57 if (!isBlank(instanceId))
58 {
59 ctx = getProductContexts(goals).get(instanceId);
60 if (ctx == null)
61 {
62 throw new MojoExecutionException("No product with instance ID '" + instanceId + "'");
63 }
64 }
65 else
66 {
67 ctx = getProductContexts(goals).get(getProductId());
68 }
69 ProductHandler product = createProductHandler(ctx.getId());
70
71 ctx.setInstallPlugin(shouldInstallPlugin());
72
73 int actualHttpPort = product.start(ctx);
74
75 getLog().info(ctx.getInstanceId() + " started successfully and available at http://localhost:" + actualHttpPort + ctx.getContextPath());
76
77 if (writePropertiesToFile)
78 {
79 properties.put("http.port", String.valueOf(actualHttpPort));
80 properties.put("context.path", ctx.getContextPath());
81 writePropertiesFile();
82 }
83
84 if (wait)
85 {
86 getLog().info("Type CTRL-C to exit");
87 try
88 {
89 while (System.in.read() != CONTROL_C)
90 {
91 }
92 }
93 catch (final IOException e)
94 {
95
96 }
97 }
98 }
99
100
101
102
103
104 private boolean shouldInstallPlugin()
105 {
106 Artifact artifact = getMavenContext().getProject().getArtifact();
107 return installPlugin &&
108 (artifact != null && !"pom".equalsIgnoreCase(artifact.getType()));
109 }
110
111 private void writePropertiesFile() throws MojoExecutionException
112 {
113 final Properties props = new Properties();
114
115 for (Map.Entry<String, String> entry : properties.entrySet())
116 {
117 props.setProperty(entry.getKey(), entry.getValue());
118 }
119
120 final File ampsProperties = new File(getMavenContext().getProject().getBuild().getDirectory(), "amps.properties");
121 OutputStream out = null;
122 try
123 {
124 out = new FileOutputStream(ampsProperties);
125 props.store(out, "");
126 }
127 catch (IOException e)
128 {
129 throw new MojoExecutionException("Error writing " + ampsProperties.getAbsolutePath(), e);
130 }
131 finally
132 {
133 IOUtils.closeQuietly(out);
134 }
135 }
136 }