forked from jfrog/artifactory-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArtifactoryMojo.java
196 lines (173 loc) · 7.44 KB
/
ArtifactoryMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package org.jfrog.buildinfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Proxy;
import org.jfrog.build.extractor.ci.BuildInfoFields;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.build.extractor.clientConfiguration.ClientProperties;
import org.jfrog.buildinfo.deployment.BuildInfoRecorder;
import org.jfrog.buildinfo.resolution.RepositoryListener;
import org.jfrog.buildinfo.resolution.ResolutionRepoHelper;
import org.jfrog.buildinfo.utils.Utils;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static org.jfrog.buildinfo.utils.Utils.getMavenVersion;
/**
* The plugin's entry point -
* Replace the resolution repositories.
* Replace the default deployment with the BuildInfoRecorder.
*/
@Mojo(name = "publish", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
public class ArtifactoryMojo extends AbstractMojo {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
private static final String[] DEPLOY_GOALS = {"deploy", "maven-deploy-plugin"};
@Parameter(required = true, defaultValue = "${project}")
MavenProject project;
@Parameter(required = true, defaultValue = "${session}")
MavenSession session;
@Component(role = RepositoryListener.class)
RepositoryListener repositoryListener;
@Parameter
Config.Artifactory artifactory = new Config.Artifactory();
@Parameter
Map<String, String> deployProperties = new HashMap<>();
@Parameter
Config.BuildInfo buildInfo = new Config.BuildInfo();
@Parameter
Config.Publisher publisher = new Config.Publisher();
@Parameter
Config.Resolver resolver = new Config.Resolver();
@Parameter
Config.Proxy proxy = new Config.Proxy();
@Override
public void execute() {
if (session.getRequest().getData().putIfAbsent("configured", Boolean.TRUE) == null) {
replaceVariables();
setupProxy();
enforceResolution();
enforceDeployment();
}
}
/**
* Replace variables in pom.xml surrounded by {{}} with environment and system variables.
*/
private void replaceVariables() {
artifactory.delegate.getAllProperties().replaceAll((key, value) -> Utils.parseInput(value));
deployProperties.replaceAll((key, value) -> Utils.parseInput(value));
}
/**
* Set up proxy from settings.xml, if not provided.
*/
private void setupProxy() {
if (this.proxy.getHost() != null) {
return;
}
Proxy proxy = session.getSettings().getActiveProxy();
if (proxy == null) {
return;
}
this.proxy.setHost(proxy.getHost());
this.proxy.setPort(proxy.getPort());
this.proxy.setUsername(proxy.getUsername());
this.proxy.setPassword(proxy.getPassword());
}
/**
* Enforce resolution from Artifactory repositories if there is a 'resolver' configuration.
*/
private void enforceResolution() {
ResolutionRepoHelper helper = new ResolutionRepoHelper(getLog(), session, artifactory.delegate);
List<ArtifactRepository> resolutionRepositories = helper.getResolutionRepositories();
if (CollectionUtils.isNotEmpty(resolutionRepositories)) {
// User configured the 'resolver'
for (MavenProject mavenProject : session.getProjects()) {
mavenProject.setPluginArtifactRepositories(resolutionRepositories);
mavenProject.setRemoteArtifactRepositories(resolutionRepositories);
}
}
}
/**
* Enforce deployment to Artifactory repositories if there is a 'publisher' configuration.
*/
private void enforceDeployment() {
if (!deployGoalExist()) {
return;
}
skipDefaultDeploy();
completeConfig();
addDeployProperties();
BuildInfoRecorder executionListener = new BuildInfoRecorder(session, getLog(), artifactory.delegate);
repositoryListener.setBuildInfoRecorder(executionListener);
session.getRequest().setExecutionListener(executionListener);
}
/**
* Return true if 'deploy' or 'maven-deploy-plugin' goals exist.
*
* @return true if 'deploy' or 'maven-deploy-plugin' goals exist
*/
private boolean deployGoalExist() {
return session.getGoals().stream().anyMatch(goal -> ArrayUtils.contains(DEPLOY_GOALS, goal));
}
/**
* Skip the default maven deploy behaviour.
*/
private void skipDefaultDeploy() {
session.getUserProperties().put("maven.deploy.skip", Boolean.TRUE.toString());
}
/**
* Complete missing configuration.
*/
private void completeConfig() {
ArtifactoryClientConfiguration.BuildInfoHandler buildInfo = this.buildInfo.delegate;
buildInfo.setBuildTimestamp(Long.toString(session.getStartTime().getTime()));
buildInfo.setBuildStarted(DATE_FORMAT.format(session.getStartTime()));
if (StringUtils.isBlank(buildInfo.getBuildName())) {
buildInfo.setBuildName(project.getArtifactId());
}
if (StringUtils.isBlank(buildInfo.getBuildNumber())) {
buildInfo.setBuildNumber(buildInfo.getBuildTimestamp());
}
buildInfo.setBuildAgentName("Maven");
buildInfo.setBuildAgentVersion(getMavenVersion(getClass()));
if (buildInfo.getBuildRetentionDays() != null) {
buildInfo.setBuildRetentionMinimumDate(buildInfo.getBuildRetentionDays().toString());
}
}
/**
* Add buildName, buildNumber, the timestamp and the configured deployProperties as a deployment properties.
* The deployer adds these properties to each of of the deployed artifacts.
*/
private void addDeployProperties() {
ArtifactoryClientConfiguration.BuildInfoHandler buildInfo = this.buildInfo.delegate;
Properties deployProperties = new Properties() {{
addDeployProperty(this, BuildInfoFields.BUILD_TIMESTAMP, buildInfo.getBuildTimestamp());
addDeployProperty(this, BuildInfoFields.BUILD_NAME, buildInfo.getBuildName());
addDeployProperty(this, BuildInfoFields.BUILD_NUMBER, buildInfo.getBuildNumber());
}};
this.deployProperties.forEach((key, value) -> addDeployProperty(deployProperties, key, value));
artifactory.delegate.fillFromProperties(deployProperties);
}
/**
* Add a single deploy property.
*
* @param deployProperties - The deploy properties collection
* @param key - The key of the property
* @param value - The value of the property
*/
private void addDeployProperty(Properties deployProperties, String key, String value) {
if (StringUtils.isNotBlank(value)) {
deployProperties.put(ClientProperties.PROP_DEPLOY_PARAM_PROP_PREFIX + key, value);
}
}
}