Skip to content

Commit 273d479

Browse files
author
tomerc
committed
1. Create helper class for extraction support.
2. enhance tests. 3. consolidate all gradle plugin in a single project 4. bug fixes in the recorder.
1 parent 1c6f7ad commit 273d479

File tree

20 files changed

+568
-231
lines changed

20 files changed

+568
-231
lines changed

Diff for: build-info-api/build.gradle

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apply plugin: 'maven'
2+
uploadArchives {
3+
repositories.mavenDeployer {
4+
name = 'gradle releases @ rjo'
5+
repository(url: "http://gradle.artifactoryonline.com/gradle/libs-snapshots-local") {
6+
authentication(userName: "jfrog", password: "gradle10")
7+
}
8+
}
9+
}

Diff for: build-info-api/src/main/java/org/jfrog/build/api/util/FileChecksumCalculator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static Map<String, String> calculateChecksums(File fileToCalculate, Strin
4747
}
4848

4949
/**
50-
* Reads the given file's checksums
50+
* Calculates the given file's checksums
5151
*
5252
* @param fileToCalculate File to calculate
5353
* @param algorithms Algorithms to use for calculation

Diff for: build-info-client/build.gradle

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apply plugin: 'maven'
2+
uploadArchives {
3+
repositories.mavenDeployer {
4+
name = 'gradle releases @ rjo'
5+
repository(url: "http://gradle.artifactoryonline.com/gradle/libs-snapshots-local") {
6+
authentication(userName: "jfrog", password: "gradle10")
7+
}
8+
}
9+
}
10+
dependencies {
11+
compile "org.apache.httpcomponents:httpclient:4.0",
12+
"org.codehaus.jackson:jackson-mapper-asl:1.4.1"
13+
project(':build-info-api')
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
buildscript {
2+
def downloadId = 'plugins-releases'
3+
String[] pathParts
4+
def artifactoryUrl
5+
if (!project.hasProperty('artifactory.url')) {
6+
if (sourceURI) {
7+
logger.debug("sourceURI: $sourceURI")
8+
String fullPath = sourceURI.path
9+
pathParts = fullPath.split("/")
10+
artifactoryUrl = "${sourceURI.scheme}://${sourceURI.authority}/${pathParts[1]}"
11+
} else {
12+
artifactoryUrl = 'http://gradle.artifactoryonline.com/gradle/'
13+
pathParts = artifactoryUrl.split("/")
14+
}
15+
} else {
16+
artifactoryUrl = project.getProperty('artifactory.url')
17+
pathParts = artifactoryUrl.split("/")
18+
}
19+
logger.debug("artifactory.url: $artifactoryUrl")
20+
21+
if (!project.hasProperty('artifactory.downloadId')) {
22+
if (pathParts.size() >= 3) {
23+
downloadId = pathParts[2]
24+
}
25+
logger.debug("no download id found using: ${downloadId}")
26+
} else {
27+
def downloadIdProperty = project.getProperty('artifactory.downloadId')
28+
logger.debug("Property artifactory.downloadId ${downloadIdProperty}")
29+
downloadId = downloadIdProperty
30+
}
31+
logger.debug("artifactory.downloadId: $downloadId")
32+
def artifactoryDownloadUrl
33+
if (!project.hasProperty('artifactory.downloadUrl')) {
34+
artifactoryDownloadUrl = artifactoryUrl + "/" + downloadId
35+
logger.debug("artifactory.downloadUrl: $artifactoryDownloadUrl")
36+
37+
} else {
38+
def artifactoryDownloadUrlProperty = project.getProperty('artifactory.downloadUrl')
39+
artifactoryDownloadUrl = artifactoryDownloadUrlProperty
40+
logger.debug("Property artifactory.downloadUrl ${artifactoryDownloadUrlProperty}")
41+
42+
}
43+
project.setProperty('artifactory.url', artifactoryUrl)
44+
project.setProperty('artifactory.downloadId', downloadId)
45+
project.setProperty('artifactory.downloadUrl', artifactoryDownloadUrl)
46+
47+
//All the tango above for this one - it configures maven repo for the project
48+
repositories {
49+
mavenRepo urls: [artifactoryDownloadUrl]
50+
}
51+
}
52+
logger.debug("Applying Artifactory Plug-in")

Diff for: build-info-extractor-gradle/build.gradle

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version = '1.0-SNAPSHOT'
2+
apply plugin: 'groovy'
3+
apply plugin: 'maven'
4+
uploadArchives {
5+
repositories.mavenDeployer {
6+
name = 'gradle releases @ rjo'
7+
repository(url: "http://gradle.artifactoryonline.com/gradle/libs-snapshots-local") {
8+
authentication(userName: "jfrog", password: "gradle10")
9+
}
10+
}
11+
}
12+
13+
dependencies {
14+
compile "org.codehaus.groovy:groovy-all:1.7.1",
15+
"com.google.collections:google-collections:1.0",
16+
"commons-lang:commons-lang:2.4",
17+
"com.google.code.findbugs:jsr305:1.3.7",
18+
"javax.annotation:jsr250-api:1.0",
19+
project(':build-info-extractor'),
20+
project(':build-info-api'),
21+
project(':build-info-client'),
22+
gradleApi()
23+
groovy localGroovy()
24+
}

Diff for: build-info-extractor-gradle/pom.xml

+23-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<artifactId>build-info-extractor</artifactId>
2121
<version>1.2.1-SNAPSHOT</version>
2222
</dependency>
23+
<dependency>
24+
<groupId>org.jfrog</groupId>
25+
<artifactId>build-info-client</artifactId>
26+
<version>1.2.1-SNAPSHOT</version>
27+
</dependency>
2328
<dependency>
2429
<groupId>org.jfrog</groupId>
2530
<artifactId>build-info-api</artifactId>
@@ -43,9 +48,14 @@
4348
<version>1.3.9</version>
4449
</dependency>
4550
<dependency>
46-
<groupId>org.gradle.artifactoryplugin</groupId>
47-
<artifactId>artifactoryplugin</artifactId>
48-
<version>0.4</version>
51+
<groupId>org.slf4j</groupId>
52+
<artifactId>slf4j-api</artifactId>
53+
<version>1.5.8</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.codehaus.groovy</groupId>
57+
<artifactId>groovy-all</artifactId>
58+
<version>1.7.1</version>
4959
</dependency>
5060
</dependencies>
5161
</dependencyManagement>
@@ -55,6 +65,10 @@
5565
<groupId>org.jfrog</groupId>
5666
<artifactId>build-info-extractor</artifactId>
5767
</dependency>
68+
<dependency>
69+
<groupId>org.jfrog</groupId>
70+
<artifactId>build-info-client</artifactId>
71+
</dependency>
5872
<dependency>
5973
<groupId>org.jfrog</groupId>
6074
<artifactId>build-info-api</artifactId>
@@ -76,14 +90,18 @@
7690
<groupId>com.google.code.findbugs</groupId>
7791
<artifactId>jsr305</artifactId>
7892
</dependency>
93+
<dependency>
94+
<groupId>org.slf4j</groupId>
95+
<artifactId>slf4j-api</artifactId>
96+
</dependency>
7997
<dependency>
8098
<groupId>org.easymock</groupId>
8199
<artifactId>easymockclassextension</artifactId>
82100
<scope>test</scope>
83101
</dependency>
84102
<dependency>
85-
<groupId>org.gradle.artifactoryplugin</groupId>
86-
<artifactId>artifactoryplugin</artifactId>
103+
<groupId>org.codehaus.groovy</groupId>
104+
<artifactId>groovy-all</artifactId>
87105
</dependency>
88106
</dependencies>
89107
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package org.jfrog.build
2+
3+
import org.gradle.api.tasks.bundling.Jar
4+
5+
import org.gradle.BuildAdapter
6+
import org.gradle.api.Action
7+
import org.gradle.api.Plugin
8+
import org.gradle.api.Project
9+
import org.gradle.api.artifacts.Dependency
10+
import org.gradle.api.invocation.Gradle
11+
import org.gradle.api.plugins.PluginContainer
12+
import org.gradle.api.specs.Spec
13+
import org.gradle.api.tasks.Upload
14+
import org.jfrog.build.api.constants.BuildInfoProperties
15+
import org.jfrog.build.extractor.gradle.BuildInfoRecorderTask
16+
import org.slf4j.Logger
17+
18+
class ArtifactoryPlugin implements Plugin<Project> {
19+
private static final Logger log = org.slf4j.LoggerFactory.getLogger(ArtifactoryPlugin.class);
20+
public static final String ENCODING = "UTF-8"
21+
22+
List<String> compatiblePlugins = ['java', 'scala', 'groovy'] as List
23+
24+
def void apply(Project project) {
25+
26+
log.debug("Using Artifactory Plugin")
27+
def artifactoryUrl = getProperty('artifactory.url', project) ?: 'http://gradle.artifactoryonline.com/gradle/'
28+
def downloadId = getProperty('artifactory.downloadId', project)
29+
if (!downloadId) {
30+
// take the target repository from the full url
31+
String[] pathParts = artifactoryUrl.split("/")
32+
if (pathParts.size() >= 3) {
33+
downloadId = pathParts[2]
34+
}
35+
//TODO: [by ys] why plugins releases in the default?
36+
downloadId = downloadId ?: 'plugins-releases'
37+
}
38+
def artifactoryDownloadUrl = getProperty('artifactory.downloadUrl', project) ?: "${artifactoryUrl}/${downloadId}"
39+
log.debug("Artifactory URL: $artifactoryUrl")
40+
log.debug("Artifactory Download ID: $downloadId")
41+
log.debug("Artifactory Download URL: $artifactoryDownloadUrl")
42+
// add artifactory url to the list of repositories
43+
project.repositories {
44+
mavenRepo urls: [artifactoryDownloadUrl]
45+
}
46+
47+
log.debug("Configuring BuildInfo task")
48+
49+
PluginContainer plugins = project.getPlugins();
50+
def isRoot = project.equals(project.getRootProject())
51+
log.debug "Configuring project ${project.name}: is root? ${isRoot}"
52+
// add the build info task only to supported projects (always include the root)
53+
plugins.matching(new Spec<Plugin>() {
54+
boolean isSatisfiedBy(Plugin plugin) {
55+
log.debug "Has plugin ${plugin.class} Is isSatisfied? ${compatiblePlugins.contains(plugin.class) || isRoot}"
56+
for (String pluginId: compatiblePlugins) {
57+
if (plugins.hasPlugin(pluginId)) {
58+
return true
59+
}
60+
}
61+
if (isRoot) {
62+
return true
63+
}
64+
return false
65+
}
66+
}).allPlugins(new Action<Plugin>() {
67+
public void execute(Plugin plugin) {
68+
log.debug "Configuring BuildInfoTask for project ${project.name} because of plugin ${plugin.class}"
69+
configureBuildInfoTask(project);
70+
}
71+
});
72+
73+
def uploadId = getProperty("artifactory.uploadId", project)
74+
if (uploadId) {
75+
// configure upload repository for maven deployer or ivy publisher
76+
log.debug "ArtifactoryURL before setting upload is ${artifactoryUrl}"
77+
log.debug("Upload ID was declared: ${uploadId} and so deployArchives task reconfigured")
78+
String uploadRootUrl = getProperty("artifactory.uploadRootUrl", project)
79+
String uploadUrl = "${artifactoryUrl}/${uploadId}"
80+
if (uploadRootUrl) {
81+
log.debug "Using Artifactory Upload Root URL: $uploadRootUrl"
82+
uploadUrl = "${uploadRootUrl}/${uploadId}"
83+
}
84+
85+
log.debug("Configure Upload URL to ${uploadUrl}")
86+
uploadUrl = appendProperties(uploadUrl, project)
87+
88+
def user = getProperty("artifactory.username", project) ?: "anonymous"
89+
def password = getProperty("artifactory.password", project) ?: ""
90+
def host = new URI(uploadUrl).getHost()
91+
project.tasks.withType(Upload.class).allObjects { uploadTask ->
92+
project.configure(uploadTask) {
93+
boolean deployIvy
94+
def deployIvyProp = getProperty("artifactory.deployIvy", project);
95+
if (deployIvyProp != null) {
96+
deployIvy = Boolean.parseBoolean(deployIvyProp);
97+
} else {
98+
deployIvy = true
99+
}
100+
if (deployIvy) {
101+
log.debug "Configuring Ivy repository"
102+
repositories {
103+
add(new org.apache.ivy.plugins.resolver.URLResolver()) {
104+
org.apache.ivy.util.url.CredentialsStore.INSTANCE.addCredentials("Artifactory Realm", host, user, password)
105+
name = 'artifactory'
106+
addIvyPattern "$uploadUrl/[organisation]/[module]/[revision]/ivy-[revision].xml"
107+
addArtifactPattern "$uploadUrl/[organisation]/[module]/[revision]/[module]-[revision].[ext]"
108+
descriptor = 'optional'
109+
checkmodified = true
110+
m2compatible = true
111+
}
112+
}
113+
}
114+
boolean deployMaven
115+
def deployMavenProp = getProperty("artifactory.deployMaven", project);
116+
if (deployMavenProp != null) {
117+
deployMaven = Boolean.parseBoolean(deployMavenProp);
118+
} else {
119+
deployMaven = true
120+
}
121+
if (deployMaven) {
122+
log.debug "Configuring Maven repository"
123+
repositories.mavenDeployer {
124+
repository(url: uploadUrl) {
125+
authentication(userName: user, password: password)
126+
}
127+
}
128+
}
129+
}
130+
}
131+
} else {
132+
if (project.getRootProject().equals(project)) {
133+
log.warn "Upload ID was not declared, no actual deployment will be performed."
134+
}
135+
}
136+
project.getGradle().addBuildListener(new BuildAdapter() {
137+
def void projectsEvaluated(Gradle gradle) {
138+
String buildStart = System.getProperty("build.start");
139+
if (!buildStart) {
140+
System.setProperty("build.start", Long.toString(System.currentTimeMillis()));
141+
}
142+
}
143+
})
144+
}
145+
146+
//TODO: [by ys] support additional matrix params by reading plugin configuration + system properties
147+
148+
String appendProperties(String uploadUrl, Project project) {
149+
String buildNumber = getProperty(BuildInfoProperties.PROP_BUILD_NUMBER, project)
150+
String buildName = getProperty(BuildInfoProperties.PROP_BUILD_NAME, project)
151+
String buildParentNumber = getProperty(BuildInfoProperties.PROP_PARENT_BUILD_NUMBER, project)
152+
String buildParentName = getProperty(BuildInfoProperties.PROP_PARENT_BUILD_NAME, project)
153+
StringBuilder builder = new StringBuilder(uploadUrl);
154+
if (buildNumber) builder.append(";${BuildInfoProperties.PROP_PARENT_BUILD_NUMBER}=${URLEncoder.encode(buildNumber, ENCODING)}")
155+
if (buildName) builder.append(";${BuildInfoProperties.PROP_BUILD_NAME}=${URLEncoder.encode(buildName, ENCODING)}")
156+
if (buildParentNumber) builder.append(";${BuildInfoProperties.PROP_PARENT_BUILD_NUMBER}=${URLEncoder.encode(buildParentNumber, ENCODING)}")
157+
if (buildParentName) builder.append(";${BuildInfoProperties.PROP_PARENT_BUILD_NAME}=${URLEncoder.encode(buildParentName, ENCODING)}")
158+
project.properties.keySet().each {String key ->
159+
if (key.startsWith('artifactory.ci.matrix')) {
160+
builder.append(";$key=${URLEncoder.encode(getProperty(key, project), ENCODING)}")
161+
}
162+
}
163+
return builder.toString()
164+
}
165+
166+
private void configureBuildInfoTask(Project project) {
167+
if (project.tasks.findByName("buildInfo")) {
168+
return
169+
}
170+
BuildInfoRecorderTask buildInfo = project.getTasks().add("buildInfo", BuildInfoRecorderTask.class)
171+
buildInfo.dependsOn({
172+
(project.getTasks().withType(Jar)).all
173+
})
174+
175+
def archivesConfiguration = project.getConfigurations().findByName(Dependency.ARCHIVES_CONFIGURATION)
176+
if (archivesConfiguration != null) {
177+
buildInfo.setConfiguration(archivesConfiguration)
178+
}
179+
buildInfo.setDescription("Generates build info from build artifacts");
180+
}
181+
182+
private String getProperty(String propertyName, Project project) {
183+
if (System.getProperty(propertyName) != null) {
184+
return System.getProperty(propertyName)
185+
}
186+
if (project.hasProperty(propertyName)) {
187+
return project.property(propertyName);
188+
} else {
189+
project = project.getParent();
190+
if (project == null) {
191+
return null;
192+
} else {
193+
return getProperty(propertyName, project);
194+
}
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)