1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.maven.plugins.contentpackage;
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.inject.Inject;
27
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.plugins.annotations.LifecyclePhase;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.plugins.annotations.ResolutionScope;
35 import org.eclipse.aether.RepositorySystem;
36 import org.eclipse.aether.RepositorySystemSession;
37 import org.eclipse.aether.repository.RemoteRepository;
38
39 import io.wcm.tooling.commons.packmgr.install.PackageInstaller;
40
41
42
43
44 @Mojo(name = "install", defaultPhase = LifecyclePhase.INSTALL, requiresProject = false, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true)
45 public final class InstallMojo extends AbstractContentPackageMojo {
46
47
48
49
50 @Parameter(property = "vault.install", defaultValue = "true")
51 private boolean install;
52
53
54
55
56
57
58
59
60
61 @Parameter(property = "vault.force")
62 private Boolean force;
63
64
65
66
67 @Parameter(property = "vault.recursive", defaultValue = "true")
68 private boolean recursive;
69
70
71
72
73
74 @Parameter(property = "vault.groupId")
75 private String groupId;
76
77
78
79
80
81 @Parameter(property = "vault.artifactId")
82 private String artifactId;
83
84
85
86
87 @Parameter(alias = "packaging", property = "vault.packaging", defaultValue = "zip")
88 private String type;
89
90
91
92
93
94 @Parameter(property = "vault.version")
95 private String version;
96
97
98
99
100 @Parameter(property = "vault.classifier")
101 private String classifier;
102
103
104
105
106 @Parameter(property = "vault.artifact")
107 private String artifact;
108
109
110
111
112
113
114
115
116
117 @Parameter(property = "vault.fileList")
118 private String packageFileList;
119
120
121
122
123 @Parameter(property = "vault.delayAfterInstallSec")
124 private Integer delayAfterInstallSec;
125
126
127
128
129 @Parameter(property = "vault.failOnNoFile", defaultValue = "true")
130 private boolean failOnNoFile;
131
132
133
134
135 @Parameter(property = "vault.replicate")
136 private boolean replicate;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 @Parameter
164 private PackageFile[] packageFiles;
165
166 @Inject
167 private RepositorySystem repoSystem;
168 @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
169 private RepositorySystemSession repoSession;
170 @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
171 private List<RemoteRepository> repositories;
172
173 @Override
174 public void execute() throws MojoExecutionException, MojoFailureException {
175 if (isSkip()) {
176 return;
177 }
178
179
180 List<io.wcm.tooling.commons.packmgr.install.PackageFile> items = new ArrayList<>();
181 ArtifactHelper helper = new ArtifactHelper(repoSystem, repoSession, repositories);
182 if (packageFiles != null && packageFiles.length > 0) {
183 for (PackageFile ref : packageFiles) {
184 io.wcm.tooling.commons.packmgr.install.PackageFile item = toPackageFile(ref, helper);
185 if (item.getFile() != null) {
186 items.add(item);
187 }
188 }
189 }
190 else if (StringUtils.isNotBlank(packageFileList)) {
191 String[] fileNames = StringUtils.split(packageFileList, ",");
192 for (String fileName : fileNames) {
193 File file = new File(StringUtils.trimToEmpty(fileName));
194 items.add(toPackageFile(file));
195 }
196 }
197 else {
198 File file = helper.getArtifactFile(artifactId, groupId, version, type, classifier, artifact);
199 if (file == null) {
200 file = getPackageFile();
201 if (file != null && !file.exists() && !failOnNoFile) {
202 file = null;
203 }
204 }
205 if (file != null) {
206 items.add(toPackageFile(file));
207 }
208 }
209
210
211 if (items.isEmpty()) {
212 if (failOnNoFile) {
213 throw new MojoExecutionException("No file found for installing.");
214 }
215 else {
216 getLog().warn("No file found for installing.");
217 }
218 return;
219 }
220
221
222 PackageInstaller installer = new PackageInstaller(getPackageManagerProperties());
223 installer.setReplicate(this.replicate);
224 installer.installFiles(items);
225 }
226
227 private io.wcm.tooling.commons.packmgr.install.PackageFile toPackageFile(PackageFile ref, ArtifactHelper helper)
228 throws MojoFailureException, MojoExecutionException {
229 io.wcm.tooling.commons.packmgr.install.PackageFile output = new io.wcm.tooling.commons.packmgr.install.PackageFile();
230
231 File file = helper.getArtifactFile(ref.getArtifactId(), ref.getGroupId(), ref.getVersion(), ref.getType(), ref.getClassifier(), ref.getArtifact());
232 if (file == null) {
233 file = ref.getPackageFile();
234 }
235 output.setFile(file);
236
237 if (ref.getInstall() != null) {
238 output.setInstall(ref.getInstall());
239 }
240 else {
241 output.setInstall(this.install);
242 }
243 if (ref.getForce() != null) {
244 output.setForce(ref.getForce());
245 }
246 else {
247 output.setForce(this.force);
248 }
249 if (ref.getRecursive() != null) {
250 output.setRecursive(ref.getRecursive());
251 }
252 else {
253 output.setRecursive(this.recursive);
254 }
255 if (ref.getDelayAfterInstallSec() != null) {
256 output.setDelayAfterInstallSec(ref.getDelayAfterInstallSec());
257 }
258 else if (this.delayAfterInstallSec != null) {
259 output.setDelayAfterInstallSec(this.delayAfterInstallSec);
260 }
261 else {
262 output.setDelayAfterInstallSecAutoDetect();
263 }
264 output.setHttpSocketTimeoutSec(ref.getHttpSocketTimeoutSec());
265
266 return output;
267 }
268
269 private io.wcm.tooling.commons.packmgr.install.PackageFile toPackageFile(File file) {
270 io.wcm.tooling.commons.packmgr.install.PackageFile output = new io.wcm.tooling.commons.packmgr.install.PackageFile();
271
272 output.setFile(file);
273 output.setInstall(this.install);
274 output.setForce(this.force);
275 output.setRecursive(this.recursive);
276 if (this.delayAfterInstallSec != null) {
277 output.setDelayAfterInstallSec(this.delayAfterInstallSec);
278 }
279
280 return output;
281 }
282
283 }