Skip to content

Commit b24ff9c

Browse files
authored
Bugfix - Gradle defaults props should not ignore deprecated build-inf… (#590)
1 parent 0bd7ff4 commit b24ff9c

File tree

13 files changed

+195
-36
lines changed

13 files changed

+195
-36
lines changed

build-info-extractor-gradle/src/test/java/org/jfrog/gradle/plugin/artifactory/Consts.java

+3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ public class Consts {
3030
static final Path GRADLE_EXAMPLE_PUBLISH = PROJECTS_ROOT.resolve("gradle-example-publish");
3131
static final Path GRADLE_KTS_EXAMPLE_PUBLISH = PROJECTS_ROOT.resolve("gradle-kts-example-publish");
3232
static final Path GRADLE_EXAMPLE_CI_SERVER = PROJECTS_ROOT.resolve("gradle-example-ci-server");
33+
static final Path DEPRECATED_GRADLE_EXAMPLE_CI_SERVER = PROJECTS_ROOT.resolve("gradle-example-ci-server-deprecated");
3334

3435
// CI example paths
3536
static final Path LIBS_DIR = GRADLE_EXTRACTOR.resolve(Paths.get("build", "libs"));
3637
static final Path INIT_SCRIPT = GRADLE_EXTRACTOR_SRC.resolve(Paths.get("main", "resources", "initscripttemplate.gradle"));
3738
static final Path BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER = PROJECTS_ROOT.resolve("buildinfo.properties.deployer");
3839
static final Path BUILD_INFO_PROPERTIES_SOURCE_RESOLVER = PROJECTS_ROOT.resolve("buildinfo.properties.resolver");
40+
static final Path DEPRECATED_BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER = PROJECTS_ROOT.resolve("deprecated.buildinfo.properties.deployer");
41+
static final Path DEPRECATED_BUILD_INFO_PROPERTIES_SOURCE_RESOLVER = PROJECTS_ROOT.resolve("deprecated.buildinfo.properties.resolver");
3942
static final Path BUILD_INFO_PROPERTIES_TARGET = TEST_DIR.toPath().resolve("buildinfo.properties");
4043

4144
// Expected artifacts

build-info-extractor-gradle/src/test/java/org/jfrog/gradle/plugin/artifactory/GradlePluginTest.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void publicationsTestKotlinDsl(String gradleVersion) throws IOException {
9797
public void ciServerTest(String gradleVersion) throws IOException {
9898
// Create test environment
9999
createTestDir(GRADLE_EXAMPLE_CI_SERVER);
100-
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "", true, true);
100+
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "", true, true, BUILD_INFO_PROPERTIES_SOURCE_RESOLVER, BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER);
101101
Map<String, String> extendedEnv = new HashMap<String, String>(envVars) {{
102102
put(BuildInfoConfigProperties.PROP_PROPS_FILE, BUILD_INFO_PROPERTIES_TARGET.toString());
103103
}};
@@ -110,11 +110,34 @@ public void ciServerTest(String gradleVersion) throws IOException {
110110
cleanTestBuilds(buildDetails.getLeft(), buildDetails.getRight(), null);
111111
}
112112

113+
/**
114+
* Gradle extractor may be run by CI servers such as Jenkins. Before the CI server runs the Gradle extractor in order to build the Gradle project,
115+
* it generated build-info properties file that contains Gradle extractor's configurations. those generated properties could be deprecated but the Gradle extractor could be the latest version (according to the build.gradle).
116+
* This test checks that the deprecated build-info properties are being handled correctly
117+
*/
118+
@Test(dataProvider = "gradleVersions")
119+
public void deprecatedCiServerTest(String gradleVersion) throws IOException {
120+
// Create test environment
121+
createTestDir(DEPRECATED_GRADLE_EXAMPLE_CI_SERVER);
122+
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "", true, true, DEPRECATED_BUILD_INFO_PROPERTIES_SOURCE_RESOLVER, DEPRECATED_BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER);
123+
Map<String, String> extendedEnv = new HashMap<String, String>(envVars) {{
124+
put(BuildInfoConfigProperties.PROP_PROPS_FILE, BUILD_INFO_PROPERTIES_TARGET.toString());
125+
}};
126+
// Run Gradle
127+
BuildResult buildResult = runGradle(gradleVersion, extendedEnv, true);
128+
// Check results
129+
// Assert all tasks ended with success outcome
130+
assertProjectsSuccess(buildResult);
131+
// Cleanup
132+
Pair<String, String> buildDetails = getBuildDetails(buildResult);
133+
cleanTestBuilds(buildDetails.getLeft(), buildDetails.getRight(), null);
134+
}
135+
113136
@Test(dataProvider = "gradleVersions")
114137
public void ciServerPublicationsTest(String gradleVersion) throws IOException {
115138
// Create test environment
116139
createTestDir(GRADLE_EXAMPLE_CI_SERVER);
117-
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "mavenJava,customIvyPublication", true, true);
140+
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "mavenJava,customIvyPublication", true, true, BUILD_INFO_PROPERTIES_SOURCE_RESOLVER, BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER);
118141
Map<String, String> extendedEnv = new HashMap<String, String>(envVars) {{
119142
put(BuildInfoConfigProperties.PROP_PROPS_FILE, BUILD_INFO_PROPERTIES_TARGET.toString());
120143
}};
@@ -131,7 +154,7 @@ public void ciServerPublicationsTest(String gradleVersion) throws IOException {
131154
public void requestedByTest(String gradleVersion) throws IOException {
132155
// Create test environment
133156
createTestDir(GRADLE_EXAMPLE_CI_SERVER);
134-
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "mavenJava,customIvyPublication", false, true);
157+
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "mavenJava,customIvyPublication", false, true, BUILD_INFO_PROPERTIES_SOURCE_RESOLVER, BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER);
135158
Map<String, String> extendedEnv = new HashMap<String, String>(envVars) {{
136159
put(BuildInfoConfigProperties.PROP_PROPS_FILE, BUILD_INFO_PROPERTIES_TARGET.toString());
137160
}};
@@ -148,7 +171,7 @@ public void requestedByTest(String gradleVersion) throws IOException {
148171
public void ciServerResolverOnlyTest(String gradleVersion) throws IOException {
149172
// Create test environment
150173
createTestDir(GRADLE_EXAMPLE_CI_SERVER);
151-
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "", false, false);
174+
generateBuildInfoProperties(getArtifactoryUrl(), getUsername(), getAdminToken(), localRepo1, virtualRepo, "", false, false, BUILD_INFO_PROPERTIES_SOURCE_RESOLVER, BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER);
152175
Map<String, String> extendedEnv = new HashMap<String, String>(envVars) {{
153176
put(BuildInfoConfigProperties.PROP_PROPS_FILE, BUILD_INFO_PROPERTIES_TARGET.toString());
154177
}};

build-info-extractor-gradle/src/test/java/org/jfrog/gradle/plugin/artifactory/Utils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ static BuildResult runGradle(String gradleVersion, Map<String, String> envVars,
8989
* @param setDeployer - Set deployer details in file
9090
* @throws IOException - In case of any IO error
9191
*/
92-
static void generateBuildInfoProperties(String contextUrl, String username, String password, String localRepo, String virtualRepo, String publications, boolean publishBuildInfo, boolean setDeployer) throws IOException {
93-
String content = generateBuildInfoPropertiesForServer(contextUrl, username, password, localRepo, virtualRepo, publications, publishBuildInfo, BUILD_INFO_PROPERTIES_SOURCE_RESOLVER);
92+
static void generateBuildInfoProperties(String contextUrl, String username, String password, String localRepo, String virtualRepo, String publications, boolean publishBuildInfo, boolean setDeployer,Path buildInfoPropertiesSourceResolver,Path buildInfoPropertiesSourceDeployer) throws IOException {
93+
String content = generateBuildInfoPropertiesForServer(contextUrl, username, password, localRepo, virtualRepo, publications, publishBuildInfo, buildInfoPropertiesSourceResolver);
9494
if (setDeployer) {
9595
content += "\n";
96-
content += generateBuildInfoPropertiesForServer(contextUrl, username, password, localRepo, virtualRepo, publications, publishBuildInfo, BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER);
96+
content += generateBuildInfoPropertiesForServer(contextUrl, username, password, localRepo, virtualRepo, publications, publishBuildInfo, buildInfoPropertiesSourceDeployer);
9797
}
9898
Files.write(BUILD_INFO_PROPERTIES_TARGET, content.getBytes(StandardCharsets.UTF_8));
9999
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Deployer settings
2+
artifactory.publish.publications=${publications}
3+
artifactory.publish.contextUrl=${contextUrl}
4+
artifactory.publish.repoKey=${localRepo}
5+
artifactory.publish.username=${username}
6+
artifactory.publish.password=${password}
7+
artifactory.publish.unstable=false
8+
artifactory.publish.buildInfo=true
9+
artifactory.publish.maven=true
10+
artifactory.publish.ivy=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Resolver settings
2+
artifactory.resolve.contextUrl=${contextUrl}
3+
artifactory.resolve.repoKey=${virtualRepo}
4+
artifactory.resolve.username=${username}
5+
artifactory.resolve.password=${password}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
def javaProjects() {
2+
subprojects.findAll { new File(it.projectDir, 'src').directory }
3+
}
4+
5+
allprojects {
6+
group = 'org.jfrog.test.gradle.publish'
7+
version = currentVersion
8+
status = 'Integration'
9+
repositories {
10+
maven {
11+
url "${System.env.BITESTS_PLATFORM_URL}/artifactory/${System.env.BITESTS_ARTIFACTORY_VIRTUAL_REPO}"
12+
credentials {
13+
username "${System.env.BITESTS_PLATFORM_USERNAME}"
14+
password "${System.env.BITESTS_PLATFORM_ADMIN_TOKEN}"
15+
}
16+
}
17+
}
18+
}
19+
20+
artifactoryPublish.skip = true
21+
22+
project('services') {
23+
artifactoryPublish.skip = true
24+
}
25+
26+
subprojects {
27+
apply plugin: 'java'
28+
apply plugin: 'maven'
29+
30+
manifest {
31+
attributes 'provider': 'gradle'
32+
}
33+
configurations {
34+
published
35+
}
36+
dependencies {
37+
testImplementation 'junit:junit:4.7'
38+
}
39+
artifacts {
40+
published file("$rootDir/gradle.properties")
41+
}
42+
}
43+
44+
configurations {
45+
published
46+
}
47+
48+
artifactory {
49+
clientConfig.setIncludeEnvVars(true)
50+
clientConfig.info.addEnvironmentProperty('test.adding.dynVar', new java.util.Date().toString())
51+
52+
contextUrl = "JENKINS_OVERRIDES_THIS_FIELD"
53+
publish {
54+
repository {
55+
repoKey = "JENKINS_OVERRIDES_THIS_FIELD" // The Artifactory repository key to publish to
56+
username = "JENKINS_OVERRIDES_THIS_FIELD" // The publisher user name
57+
password = "JENKINS_OVERRIDES_THIS_FIELD" // The publisher password
58+
// This is an optional section for configuring Ivy publication (when publishIvy = true).
59+
ivy {
60+
ivyLayout = '[organization]/[module]/ivy-[revision].xml'
61+
artifactLayout = '[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]'
62+
mavenCompatible = true
63+
//Convert any dots in an [organization] layout value to path separators, similar to Maven's groupId-to-path conversion. True if not specified
64+
}
65+
}
66+
defaults {
67+
// Reference to Gradle configurations defined in the build script.
68+
// This is how we tell the Artifactory Plugin which artifacts should be
69+
// published to Artifactory.
70+
publishConfigs('archives', 'published')
71+
// Properties to be attached to the published artifacts.
72+
properties = ['qa.level': 'basic', 'dev.team': 'core']
73+
// You can also attach properties to published artifacts according to
74+
// the following notation:
75+
// <configuration name> <artifact spec>
76+
// The <configuration name> should be the name of the relevant Gradle Configuration or 'all'
77+
// (for all configurations).
78+
// Artifact spec has the following structure:
79+
// group:artifact:version:classifier@ext
80+
// Any element in the artifact spec notation can contain the * and ? wildcards.
81+
// For example:
82+
// org.acme:*:1.0.?_*:*@tgz
83+
properties {
84+
all '*:*:1.*:*@*', key1: 'val1', key2: 'val2'
85+
all 'org.jfrog.*:*:1.*:*@jar*', key3: 'val3', key4: 'val4'
86+
}
87+
publishPom = true // Publish generated POM files to Artifactory (true by default)
88+
publishIvy = true // Publish generated Ivy descriptor files to Artifactory (true by default)
89+
}
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
currentVersion=1.0-SNAPSHOT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include "shared", "api", "services:webservice"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.gradle.shared;
2+
3+
import java.io.IOException;
4+
import java.util.Properties;
5+
6+
public class Person {
7+
private String name;
8+
9+
public Person(String name) {
10+
this.name = name;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public String readProperty() throws IOException {
22+
Properties properties = new Properties();
23+
properties.load(getClass().getClassLoader().getResourceAsStream("org/gradle/shared/main.properties"));
24+
return properties.getProperty("main");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* These are the shared classes.
3+
*/
4+
package org.gradle.shared;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main=mainValue

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/ArtifactoryClientConfiguration.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,12 @@ public ImmutableMap<String, String> getMatrixParams() {
923923
if (calculatedMatrixParams != null) {
924924
return calculatedMatrixParams;
925925
}
926-
Map<String, String> result = getResolveMatrixParams(getMatrixParamPrefix());
926+
// First, get value using deprecated key.
927+
// This check must be first, otherwise, build.gradle properties will override the CI (e.g Jenkins / teamcity) properties.
928+
Map<String, String> result = getResolveMatrixParams(getDeprecatedMatrixParamPrefix());
927929
if (result.size() == 0) {
928-
result = getResolveMatrixParams(getDeprecatedMatrixParamPrefix());
930+
// Fallback to none deprecated key.
931+
result = getResolveMatrixParams(getMatrixParamPrefix());
929932
}
930933
this.calculatedMatrixParams = ImmutableMap.copyOf(result);
931934
return calculatedMatrixParams;

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/PrefixPropertyHandler.java

+18-27
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,10 @@ public String getStringValue(String key) {
4747
}
4848

4949
public String getStringValue(String key, String def) {
50-
String value = props.get(prefix + key);
50+
String value = getValueWithFallback(key);
5151
if (StringUtils.isNotBlank(value)) {
5252
return value;
5353
}
54-
// Fallback, try to get value with deprecated key.
55-
// This may happen if a newer version of Build-Info is used old CI which generates the deprecated properties.
56-
value = props.get(ARTIFACTORY_PREFIX + prefix + key);
57-
if (StringUtils.isNotBlank(value)) {
58-
return value;
59-
}
60-
6154
return def;
6255
}
6356

@@ -70,19 +63,11 @@ public void setStringValue(String key, String value) {
7063
}
7164

7265
public Boolean getBooleanValue(String key, Boolean def) {
73-
String value = props.get(prefix + key);
74-
// TODO: throw exception if not true or false. If prop set to something else
66+
String value = getValueWithFallback(key);
7567
Boolean result = (value == null) ? null : Boolean.parseBoolean(value);
7668
if (result != null) {
7769
return result;
7870
}
79-
// Fallback, try to get value with deprecated key.
80-
// This may happen if a newer version of Build-Info is used old CI which generates the deprecated properties.
81-
value = props.get(ARTIFACTORY_PREFIX + prefix + key);
82-
result = (value == null) ? null : Boolean.parseBoolean(value);
83-
if (result != null) {
84-
return result;
85-
}
8671
return def;
8772
}
8873

@@ -99,31 +84,37 @@ public Integer getIntegerValue(String key) {
9984
}
10085

10186
public Integer getIntegerValue(String key, Integer def) {
102-
Integer result = getInteger(key, prefix);
103-
if (result != null) {
104-
return result;
105-
}
106-
// Fallback, try to get value with deprecated key.
107-
// This may happen if a newer version of Build-Info is used old CI which generates the deprecated properties.
108-
result = getInteger(key, ARTIFACTORY_PREFIX + prefix);
87+
Integer result = getInteger(key);
10988
if (result != null) {
11089
return result;
11190
}
11291
return def;
11392
}
11493

115-
private Integer getInteger(String key, String targetPrefix) {
94+
private Integer getInteger(String key) {
11695
Integer result;
117-
String s = props.get(targetPrefix + key);
96+
String s = getValueWithFallback(key);
11897
if (s != null && !StringUtils.isNumeric(s)) {
119-
log.debug("Property '" + targetPrefix + key + "' is not of numeric value '" + s + "'");
98+
log.debug("Property '" + key + "' is not of numeric value '" + s + "'");
12099
result = null;
121100
} else {
122101
result = (s == null) ? null : Integer.parseInt(s);
123102
}
124103
return result;
125104
}
126105

106+
private String getValueWithFallback(String key) {
107+
// First, get value using deprecated key.
108+
// This check must be first, otherwise, build.gradle properties will override the CI (e.g Jenkins / teamcity) properties.
109+
String value = props.get(ARTIFACTORY_PREFIX + prefix + key);
110+
if (StringUtils.isNotBlank(value)) {
111+
return value;
112+
}
113+
// Fallback to none deprecated key.
114+
return props.get(prefix + key);
115+
}
116+
117+
127118
public void setIntegerValue(String key, Integer value) {
128119
if (value == null) {
129120
props.remove(prefix + key);

0 commit comments

Comments
 (0)