Skip to content

Commit

Permalink
add support for snapshot artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
LCLPYT committed Nov 1, 2023
1 parent 078f363 commit b0e5ab5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 30 deletions.
19 changes: 19 additions & 0 deletions src/main/java/work/lclpnet/build/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package work.lclpnet.build;

public class Constants {

public static final String
PROP_VERSION_OVERRIDE = "versionOverride",
PROP_MAVEN_USER = "mavenUser",
PROP_MAVEN_PASSWORD = "mavenPassword",
PROP_MAVEN_URL = "mavenUrl",
PROP_SNAPSHOT_URL = "snapshotUrl",
PROP_BUILD_LOCAL = "build.local",
ENV_CI_VERSION = "CI_VERSION",
ENV_DEPLOY_URL = "DEPLOY_URL",
ENV_DEPLOY_USER = "DEPLOY_USER",
ENV_DEPLOY_PASSWORD = "DEPLOY_PASSWORD",
ENV_GITHUB_REF = "GITHUB_REF";

private Constants() {}
}
7 changes: 7 additions & 0 deletions src/main/java/work/lclpnet/build/ext/BuildUtilsExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public interface BuildUtilsExtension {

Property<String> getVersionPattern();

/**
* Loads properties from a source, such as a file or filename.
* The properties are loaded into this extension.
* Any existing properties are replaced by the new values.
* @param src The property source, can be a {@link java.io.File} or file name.
* @return All properties that were loaded to this point-
*/
Properties loadProperties(Object src);

void setupPublishRepository(RepositoryHandler repositories, Properties properties);
Expand Down
87 changes: 57 additions & 30 deletions src/main/java/work/lclpnet/build/ext/BuildUtilsExtensionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import java.util.Properties;
import java.util.stream.Stream;

import static work.lclpnet.build.Constants.*;

public class BuildUtilsExtensionImpl implements BuildUtilsExtension {

private final Project project;
private final Property<String> versionPattern;
private final Properties properties = new Properties();
private GitVersionResolver gitVersionResolver = null;
private String latestTag = null;
private volatile String latestTag = null;

public BuildUtilsExtensionImpl(Project project) {
this.project = project;
Expand All @@ -28,12 +31,12 @@ public BuildUtilsExtensionImpl(Project project) {
public String latestTag() {
Map<String, String> env = System.getenv();

if (env.containsKey("CI_VERSION")) {
return env.get("CI_VERSION");
if (env.containsKey(ENV_CI_VERSION)) {
return env.get(ENV_CI_VERSION);
}

if (env.containsKey("GITHUB_REF")) {
String githubRef = env.get("GITHUB_REF");
if (env.containsKey(ENV_GITHUB_REF)) {
String githubRef = env.get(ENV_GITHUB_REF);

String prefix = "refs/tags/";

Expand All @@ -42,22 +45,24 @@ public String latestTag() {
}
}

if (latestTag != null) return latestTag;

return fetchLatestTag();
}

@Override
public String gitVersion() {
final String version = latestTag();
String version = latestTag();

String versionPattern = getVersionPattern().get();
if (!version.matches(versionPattern)) {
throw new IllegalStateException(String.format("Latest tag '%s' does not match the required versioning scheme", version));
}

if ("true".equals(System.getProperty("build.local"))) {
return version + "+local";
if ("true".equals(System.getProperty(PROP_BUILD_LOCAL))) {
version += "+local";
}

if (properties.containsKey(PROP_VERSION_OVERRIDE)) {
version = properties.getProperty(PROP_VERSION_OVERRIDE);
}

project.getLogger().lifecycle("Project version: {}", version);
Expand All @@ -80,60 +85,82 @@ public Properties loadProperties(Object src) {
String.class.getName()));
}

final Properties props = new Properties();

if (file.exists()) {
try (InputStream in = Files.newInputStream(file.toPath())) {
props.load(in);
properties.load(in);
} catch (Exception ex) {
throw new IllegalStateException(String.format("Error loading %s", file.getAbsolutePath()), ex);
}
}

return props;
return properties;
}

@Override
public Property<String> getVersionPattern() {
return versionPattern;
}

private synchronized String fetchLatestTag() {
if (latestTag != null) return latestTag;

File pwd = project.getProjectDir();
private String fetchLatestTag() {
if (latestTag != null) {
return latestTag;
}

synchronized (this) {
if (latestTag != null) {
return latestTag;
}

if (gitVersionResolver == null) {
gitVersionResolver = new GitVersionResolver(project.getLogger());
}
}

return gitVersionResolver.getGitVersion(pwd);
File pwd = project.getProjectDir();

latestTag = gitVersionResolver.getGitVersion(pwd);

return latestTag;
}
}

@Override
public void setupPublishRepository(RepositoryHandler repositories, Properties props) {
repositories.maven(repo -> {
Map<String, String> env = System.getenv();

if (Stream.of("DEPLOY_URL", "DEPLOY_USER", "DEPLOY_PASSWORD").allMatch(env::containsKey)) {
if (Stream.of(ENV_DEPLOY_URL, ENV_DEPLOY_USER, ENV_DEPLOY_PASSWORD).allMatch(env::containsKey)) {
repo.credentials(credentials -> {
credentials.setUsername(env.get("DEPLOY_USER"));
credentials.setPassword(env.get("DEPLOY_PASSWORD"));
credentials.setUsername(env.get(ENV_DEPLOY_USER));
credentials.setPassword(env.get(ENV_DEPLOY_PASSWORD));
});

repo.setUrl(env.get("DEPLOY_URL"));
} else if (Stream.of("mavenHost", "mavenUser", "mavenPassword").allMatch(props::containsKey)) {
repo.setUrl(env.get(ENV_DEPLOY_URL));
return;
}

if (project.getVersion().toString().endsWith("-SNAPSHOT") && Stream.of(PROP_SNAPSHOT_URL, PROP_MAVEN_USER,
PROP_MAVEN_PASSWORD).allMatch(props::containsKey)) {

repo.credentials(credentials -> {
credentials.setUsername(props.getProperty("mavenUser"));
credentials.setPassword(props.getProperty("mavenPassword"));
credentials.setUsername(props.getProperty(PROP_MAVEN_USER));
credentials.setPassword(props.getProperty(PROP_MAVEN_PASSWORD));
});

repo.setUrl(props.getProperty("mavenHost"));
} else {
repo.setUrl("file:///" + project.getProjectDir().getAbsolutePath() + "/repo");
repo.setUrl(props.getProperty(PROP_SNAPSHOT_URL));
return;
}

if (Stream.of(PROP_MAVEN_URL, PROP_MAVEN_USER, PROP_MAVEN_PASSWORD).allMatch(props::containsKey)) {
repo.credentials(credentials -> {
credentials.setUsername(props.getProperty(PROP_MAVEN_USER));
credentials.setPassword(props.getProperty(PROP_MAVEN_PASSWORD));
});

repo.setUrl(props.getProperty(PROP_MAVEN_URL));
return;
}

repo.setUrl("file:///" + project.getProjectDir().getAbsolutePath() + "/repo");
});
}
}

0 comments on commit b0e5ab5

Please sign in to comment.