1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.commonjava.maven.plugins.execroot;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.io.StringWriter;
22
23 import org.apache.maven.execution.MavenSession;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.project.MavenProject;
29
30
31
32
33 public abstract class AbstractDirectoryGoal extends AbstractMojo {
34
35
36 @Parameter(defaultValue = "dirProperty", required = true)
37 protected String property;
38
39
40 @Parameter(defaultValue = "${project}", readonly = true)
41 protected MavenProject currentProject;
42
43
44 @Parameter(defaultValue = "${session}", readonly = true)
45 protected MavenSession session;
46
47
48 @Parameter(defaultValue = "false")
49 protected boolean quiet;
50
51
52 @Parameter(defaultValue = "false")
53 protected boolean systemProperty;
54
55
56 private final Object lockObj = new Object();
57
58
59
60
61
62
63 @Parameter(defaultValue = "false", property = "directory.skip")
64 private boolean skip;
65
66 @Override
67 public final void execute() throws MojoExecutionException, MojoFailureException {
68
69 if (this.skip) {
70 getLog().info("Directory Plugin is skipped");
71 return;
72 }
73
74 File execRoot;
75 synchronized (lockObj) {
76 final String key = getContextKey();
77 execRoot = (File) getPluginContext().get(key);
78 if (execRoot == null) {
79 execRoot = findDirectory();
80 getPluginContext().put(key, execRoot);
81 }
82 }
83
84 if (!quiet) {
85 getLog().info(getLogLabel() + " set to: " + execRoot);
86 }
87
88 currentProject.getProperties().setProperty(property, execRoot.getAbsolutePath());
89
90 if (systemProperty) {
91 String existingValue = System.getProperty(property);
92 if (existingValue == null) {
93 System.setProperty(property, execRoot.getAbsolutePath());
94 }
95 }
96
97 if (getLog().isDebugEnabled()) {
98 try (StringWriter str = new StringWriter();
99 PrintWriter print = new PrintWriter(str)) {
100 currentProject.getProperties().list(print);
101
102 getLog().debug("After setting property '" + property + "', project properties are:\n\n" + str);
103 } catch (IOException e) {
104 throw new MojoExecutionException("Failed to write properties", e);
105 }
106 }
107 }
108
109
110
111
112
113
114 protected abstract String getLogLabel();
115
116
117
118
119
120
121
122
123
124 protected abstract File findDirectory() throws MojoExecutionException;
125
126
127
128
129
130
131 protected abstract String getContextKey();
132
133 }