1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.nicoulaj.maven.plugins.vagrant;
17
18 import de.saumya.mojo.gem.AbstractGemMojo;
19 import de.saumya.mojo.jruby.AbstractJRubyMojo;
20 import de.saumya.mojo.ruby.gems.GemManager;
21 import de.saumya.mojo.ruby.script.Script;
22 import de.saumya.mojo.ruby.script.ScriptException;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugin.descriptor.PluginDescriptor;
27 import org.apache.maven.plugins.annotations.Component;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.repository.RepositorySystem;
31 import org.codehaus.classworlds.ClassRealm;
32 import org.codehaus.plexus.archiver.UnArchiver;
33 import org.sonatype.plexus.build.incremental.BuildContext;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.lang.reflect.Field;
38
39 import static java.util.Arrays.asList;
40 import static org.codehaus.plexus.util.StringUtils.isNotBlank;
41
42
43
44
45
46
47
48 abstract class AbstractVagrantMojo extends AbstractGemMojo {
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Parameter(defaultValue = "${project.build.directory}/vagrant/gems")
63 protected File gemHome;
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Parameter(defaultValue = "${project.build.directory}/vagrant/vagrant.d")
78 protected File vagrantHome;
79
80
81
82
83
84
85
86
87
88
89
90
91
92 @Parameter(defaultValue = "${project.build.directory}/vagrant/vagrantrc")
93 protected File vagrantRc;
94
95
96
97
98 @Parameter( defaultValue = "${project}", readonly = true )
99 protected MavenProject project;
100
101
102
103
104 @Parameter( defaultValue = "${plugin}", readonly = true )
105 protected PluginDescriptor plugin;
106
107
108
109
110 @Component
111 protected RepositorySystem repositorySystem;
112
113
114
115
116 @Parameter( readonly = true, defaultValue="${localRepository}" )
117 protected ArtifactRepository localRepository;
118
119
120
121
122 @Parameter( readonly = true, defaultValue="${dummy}" )
123 protected ClassRealm classRealm;
124
125
126
127
128
129
130 @Component(hint="zip")
131 protected UnArchiver unzip;
132
133
134
135
136 @Component
137 protected GemManager manager;
138
139
140
141
142 @Component
143 protected BuildContext buildContext;
144
145
146 private void setup() throws MojoFailureException {
147 super.project = this.project;
148 super.localRepository = this.localRepository;
149 super.classRealm = this.classRealm;
150 super.repositorySystem = this.repositorySystem;
151 super.jrubyFork = true;
152 super.jrubyVerbose = false;
153 super.unzip = unzip;
154 super.plugin = plugin;
155 super.includeOpenSSL = true;
156 super.includeRubygemsInTestResources = false;
157 super.installRDoc = false;
158 super.installRI = false;
159 super.gemUseSystem = false;
160 super.gemHome = this.gemHome;
161 super.gemPath = this.gemHome;
162 super.binDirectory = new File(gemHome.getAbsolutePath() + "-" + plugin.getArtifactId(), "bin");
163 super.supportNative = true;
164 super.manager = this.manager;
165 try {
166 Field field = AbstractJRubyMojo.class.getDeclaredField("buildContext");
167 field.setAccessible(true);
168 field.set(this, buildContext);
169 } catch (NoSuchFieldException e) {
170 throw new MojoFailureException("Mojo configuration failed",e);
171 } catch (IllegalAccessException e) {
172 throw new MojoFailureException("Mojo configuration failed",e);
173 }
174 }
175
176 @Override
177 public final void execute() throws MojoExecutionException, MojoFailureException {
178 setup();
179 super.execute();
180 }
181
182 @Override
183 protected final void executeWithGems() throws MojoExecutionException, MojoFailureException {
184 try {
185 doExecute();
186 } catch (IOException e) {
187 throw new MojoFailureException("Vagrant execution failed", e);
188 } catch (ScriptException e) {
189 throw new MojoFailureException("Vagrant execution failed", e);
190 }
191 }
192
193 abstract protected void doExecute() throws IOException, ScriptException;
194
195 protected final void cli(String... args) throws IOException, ScriptException {
196 cli(asList(args));
197 }
198
199 protected final void cli(Iterable<String> args) throws IOException, ScriptException {
200 factory.addEnv("VAGRANT_HOME", vagrantHome);
201 factory.addEnv("VAGRANT_RC", vagrantRc);
202 final Script script = factory.newScriptFromSearchPath("vagrant");
203 for (String arg : args) if (isNotBlank(arg)) script.addArg(arg);
204 script.execute();
205 }
206 }