diff --git a/.gitignore b/.gitignore index 9dc0a80..3595b04 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,8 @@ replay_pid* **/build/ !src/**/build/ +gradle.properties + # Ignore Gradle GUI config gradle-app.setting diff --git a/app/build.gradle b/app/build.gradle index 317b5e8..66bd5ad 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,14 +6,14 @@ * User Manual available at https://docs.gradle.org/7.3/userguide/building_java_projects.html */ -buildscript { - repositories { - jcenter() - } - dependencies { - classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2" - } -} +// buildscript { +// repositories { +// jcenter() +// } +// dependencies { +// classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2" +// } +// } plugins { // Apply the application plugin to add support for building a CLI application in Java. @@ -27,11 +27,11 @@ plugins { -apply plugin: 'com.github.johnrengelman.shadow' +// apply plugin: 'com.github.johnrengelman.shadow' group = 'io.github.mainmethod0126' -version = "0.1.2" +version = "0.1.4" sourceCompatibility = 11 targetCompatibility = 11 @@ -39,7 +39,6 @@ tasks.withType(Javadoc) { options.encoding = 'UTF-8' } - compileJava { options.encoding = 'UTF-8' } diff --git a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/SemanticVersionManager.java b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/SemanticVersionManager.java index 21db135..037175c 100644 --- a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/SemanticVersionManager.java +++ b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/SemanticVersionManager.java @@ -22,7 +22,9 @@ public void apply(Project project) { this.project = project; - initExtensions(); + project.getExtensions().create("ssv", + SimpleSemanticVersionPluginExtension.class); + initUtils(); BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning", @@ -49,11 +51,14 @@ public void apply(Project project) { buildAndVersioning.setBm(bm); buildAndVersioning.setProject(project); - try { - buildAndVersioning.doExcute(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + + project.afterEvaluate(pj -> { + try { + buildAndVersioning.doExcute(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); project.getGradle().addBuildListener(new BuildListener() { @@ -87,10 +92,6 @@ public void settingsEvaluated(Settings buildResult) { } - private void initExtensions() { - SimpleSemanticVersionPluginExtension.init(this.project); - } - private void initUtils() { SsvPaths.init(this.project); } diff --git a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/extension/SimpleSemanticVersionPluginExtension.java b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/extension/SimpleSemanticVersionPluginExtension.java index 483c5aa..7b7424b 100644 --- a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/extension/SimpleSemanticVersionPluginExtension.java +++ b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/extension/SimpleSemanticVersionPluginExtension.java @@ -1,64 +1,8 @@ package io.github.mainmethod0126.gradle.simple.versioning.extension; -import org.gradle.api.Project; +import org.gradle.api.provider.Property; -import io.github.mainmethod0126.gradle.simple.versioning.utils.DateUtils; +public interface SimpleSemanticVersionPluginExtension { + Property getIsDateInBuildArtifactDirPath(); -/** - * - * I wanted to create it as a singleton object, but it seemed like we would - * always have to pass the 'project' object as an argument when calling - * {@code getInstance()} from outside. Therefore, I created the object to be - * conveniently used after calling the initial {@code initExtension()} function. - * - * warn! : This class was not designed with consideration for multi-threading - * and is not thread-safe. Please be cautious when using it in a multi-threaded - * environment. - */ -public class SimpleSemanticVersionPluginExtension { - - /** - * This is not a singleton object, but rather a global variable used for the - * convenience of users. - */ - private static SimpleSemanticVersionPluginExtension extension; - - public static void init(Project project) { - if (extension == null) { - extension = project.getExtensions().create("ssv", - SimpleSemanticVersionPluginExtension.class); - } - } - - public static SimpleSemanticVersionPluginExtension getExtension() { - return extension; - } - - private String buildDate = DateUtils.getCurrentDate(DateUtils.DateUnit.DAY); - private boolean isDateInBuildArtifactDirPath = false; - private String applicationVersion = "0.0.0"; - - public boolean isDateInBuildArtifactDirPath() { - return isDateInBuildArtifactDirPath; - } - - public void setDateInBuildPath(boolean isDateInBuildArtifactDirPath) { - this.isDateInBuildArtifactDirPath = isDateInBuildArtifactDirPath; - } - - public String getBuildDate() { - return buildDate; - } - - public void setBuildDate(String buildDate) { - this.buildDate = buildDate; - } - - public String getApplicationVersion() { - return applicationVersion; - } - - public void setApplicationVersion(String applicationVersion) { - this.applicationVersion = applicationVersion; - } -} +} \ No newline at end of file diff --git a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/task/BuildAndVersioning.java b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/task/BuildAndVersioning.java index 32168ce..d3a2123 100644 --- a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/task/BuildAndVersioning.java +++ b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/task/BuildAndVersioning.java @@ -19,14 +19,14 @@ import org.gradle.api.tasks.bundling.Jar; import org.json.JSONObject; -import io.github.mainmethod0126.gradle.simple.versioning.extension.SimpleSemanticVersionPluginExtension; import io.github.mainmethod0126.gradle.simple.versioning.task.version.SemanticVersionFile; +import io.github.mainmethod0126.gradle.simple.versioning.utils.DateUtils; +import io.github.mainmethod0126.gradle.simple.versioning.utils.DateUtils.DateUnit; import io.github.mainmethod0126.gradle.simple.versioning.utils.NumberUtils; import io.github.mainmethod0126.gradle.simple.versioning.utils.SsvPaths; public class BuildAndVersioning extends DefaultTask { - @Inject private Project project; @Input @@ -49,6 +49,30 @@ public class BuildAndVersioning extends DefaultTask { private SemanticVersionFile semanticVersionFile; + private String buildDate; + + @Inject + public BuildAndVersioning(Project project) { + + // set default application version + Path versionFilePath = Paths.get("version.json"); + try { + this.semanticVersionFile = new SemanticVersionFile(versionFilePath); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + + // set buildDate + this.buildDate = DateUtils.getCurrentDate(DateUnit.DAY); + + // set default java version + String sourceCompatibility = this.javav; + if (sourceCompatibility.isEmpty()) { + sourceCompatibility = System.getProperty("java.version"); + } + project.setProperty("sourceCompatibility", sourceCompatibility); + } + /** * default version 정보가 적혀있는 version.json 파일을 생성합니다. * @@ -138,37 +162,22 @@ public void taskParamResolve(final TaskParam taskParam) { printVersionChangeInfo("buildMetadata", prevBm, nextBm); } - private void init() throws IOException { - - // set default application version - Path versionFilePath = Paths.get("version.json"); - semanticVersionFile = new SemanticVersionFile(versionFilePath); - SimpleSemanticVersionPluginExtension.getExtension().setApplicationVersion(semanticVersionFile.getFullString()); - - // set default java version - String sourceCompatibility = this.javav; - if (sourceCompatibility.isEmpty()) { - sourceCompatibility = System.getProperty("java.version"); - } - project.setProperty("sourceCompatibility", sourceCompatibility); - } - private Artifact createArtifact() throws IOException { - String applicationVersion = SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion(); + String applicationVersion = semanticVersionFile.getFullString(); - if (SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion() == null) { + if (applicationVersion == null) { throw new NullPointerException("applicationVersion is null"); } - Path buildDirPath = SsvPaths.getBuildDir(); + Path buildDirPath = SsvPaths.getBuildDir(this.buildDate, applicationVersion); project.setProperty("version", applicationVersion); mkdir(buildDirPath); setJar(buildDirPath.toAbsolutePath().toString(), applicationVersion, - SimpleSemanticVersionPluginExtension.getExtension().getBuildDate()); + this.buildDate); return new Artifact(buildDirPath, applicationVersion); @@ -195,8 +204,6 @@ private void setJar(String buildDirPath, String applicationVersion, String build @TaskAction public void doExcute() throws IOException { - init(); - TaskParam userInputVersion = new TaskParam(this.javav, this.major, this.minor, this.patch, this.pr, this.bm); taskParamResolve(userInputVersion); diff --git a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/utils/SsvPaths.java b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/utils/SsvPaths.java index c47c8ec..a0800eb 100644 --- a/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/utils/SsvPaths.java +++ b/app/src/main/java/io/github/mainmethod0126/gradle/simple/versioning/utils/SsvPaths.java @@ -19,14 +19,22 @@ public static void init(Project pj) { project = pj; } - public static Path getBuildDir() { - if (SimpleSemanticVersionPluginExtension.getExtension().isDateInBuildArtifactDirPath()) { + public static Path getBuildDir(String buildDate, String applicationVersion) { + + SimpleSemanticVersionPluginExtension extension = (SimpleSemanticVersionPluginExtension) project.getExtensions() + .findByName("ssv"); + + if (extension == null) { + throw new IllegalStateException("Could not find SimpleSemanticVersionPluginExtension"); + } + + if (extension.getIsDateInBuildArtifactDirPath().getOrElse(false).booleanValue()) { return Paths.get(project.getProjectDir().toString(), "dist", - SimpleSemanticVersionPluginExtension.getExtension().getBuildDate(), - SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion()); + buildDate, + applicationVersion); } else { return Paths.get(project.getProjectDir().toString(), "dist", - SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion()); + applicationVersion); } } diff --git a/app/src/test/java/gradle/simple/versioning/SemanticVersionManagerTest.java b/app/src/test/java/gradle/simple/versioning/SemanticVersionManagerTest.java index 7cd58ed..0b25396 100644 --- a/app/src/test/java/gradle/simple/versioning/SemanticVersionManagerTest.java +++ b/app/src/test/java/gradle/simple/versioning/SemanticVersionManagerTest.java @@ -27,7 +27,8 @@ void buildAndVersioningTaskTest() throws IOException { BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning", BuildAndVersioning.class); - SimpleSemanticVersionPluginExtension.init(project); + project.getExtensions().create("ssv", + SimpleSemanticVersionPluginExtension.class); SsvPaths.init(project); // when, then @@ -45,7 +46,8 @@ void buildAndVersioningTask_whenBuildSucceed_thenIncreaseCommitTest() throws IOE BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning", BuildAndVersioning.class); - SimpleSemanticVersionPluginExtension.init(project); + project.getExtensions().create("ssv", + SimpleSemanticVersionPluginExtension.class); SsvPaths.init(project); buildAndVersioning.setMajor("++"); @@ -73,8 +75,9 @@ void buildAndVersioningTask_whenBuildFailed_thenNotIncresaseCommitTest() throws BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning", BuildAndVersioning.class); - SimpleSemanticVersionPluginExtension.init(project); - SimpleSemanticVersionPluginExtension.getExtension().setDateInBuildPath(false); + SimpleSemanticVersionPluginExtension extension = project.getExtensions().create("ssv", + SimpleSemanticVersionPluginExtension.class); + extension.getIsDateInBuildArtifactDirPath().set(false); SsvPaths.init(project); buildAndVersioning.setMajor("++");