├── samples ├── example │ ├── .gitignore │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── App.java │ ├── README.md │ ├── build.gradle │ ├── gradlew.bat │ └── gradlew └── spring-boot │ ├── .gitignore │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── src │ └── main │ │ └── java │ │ └── com │ │ └── example │ │ └── App.java │ ├── build.gradle │ ├── gradlew.bat │ └── gradlew ├── .gitignore ├── gradle ├── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── version.gradle └── variables.properties ├── src ├── main │ ├── resources │ │ └── META-INF │ │ │ └── gradle-plugins │ │ │ └── com.github.ksoichiro.build.info.properties │ └── groovy │ │ └── com │ │ └── github │ │ └── ksoichiro │ │ └── build │ │ └── info │ │ ├── GitInfo.groovy │ │ ├── BuildInfoPlugin.groovy │ │ ├── BuildInfoExtension.groovy │ │ └── GenerateBuildInfoTask.groovy └── test │ └── groovy │ └── com │ └── github │ └── ksoichiro │ └── build │ └── info │ ├── PluginNoGitDirSpec.groovy │ ├── MultiProjectFunctionalSpec.groovy │ ├── PluginSpec.groovy │ └── FunctionalSpec.groovy ├── appveyor.yml ├── .editorconfig ├── .travis.yml ├── gradlew.bat ├── README.md ├── README.template.md ├── gradlew └── LICENSE /samples/example/.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | -------------------------------------------------------------------------------- /samples/spring-boot/.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | .idea/ 3 | /build/ 4 | *.iml 5 | *.swp 6 | samples/repo/ 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksoichiro/gradle-build-info-plugin/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/resources/META-INF/gradle-plugins/com.github.ksoichiro.build.info.properties: -------------------------------------------------------------------------------- 1 | implementation-class=com.github.ksoichiro.build.info.BuildInfoPlugin 2 | -------------------------------------------------------------------------------- /samples/example/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksoichiro/gradle-build-info-plugin/HEAD/samples/example/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /samples/spring-boot/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksoichiro/gradle-build-info-plugin/HEAD/samples/spring-boot/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /samples/example/src/main/java/com/example/App.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | public class App { 4 | public static void main(String[] args) { 5 | System.out.println("Hello, world!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/groovy/com/github/ksoichiro/build/info/GitInfo.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | class GitInfo { 4 | boolean missing 5 | boolean valid 6 | String commit 7 | String branch 8 | String committerDate 9 | } 10 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: "{build}" 2 | 3 | build: 4 | verbosity: detailed 5 | 6 | build_script: 7 | - gradlew.bat --full-stacktrace clean build 8 | 9 | test_script: 10 | - gradlew.bat --full-stacktrace check 11 | 12 | cache: 13 | - .gradle 14 | - C:\Users\appveyor\.gradle 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Dec 13 13:39:46 JST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [.travis.yml] 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /samples/example/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Dec 13 13:39:46 JST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip 7 | -------------------------------------------------------------------------------- /samples/spring-boot/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Dec 13 13:39:46 JST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip 7 | -------------------------------------------------------------------------------- /samples/example/README.md: -------------------------------------------------------------------------------- 1 | You can check your MANIFEST.MF in your JAR: 2 | 3 | ``` 4 | $ ./gradlew build 5 | $ unzip -p build/libs/example.jar META-INF/MANIFEST.MF 6 | Manifest-Version: 1.0 7 | Git-Commit: c57cfad 8 | Git-Committer-Date: 2015-12-13 15:12:35 +0900 9 | Build-Date: 2015-12-13 15:12:47 +0900 10 | ``` 11 | -------------------------------------------------------------------------------- /samples/example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | maven { 5 | url uri('../repo') 6 | } 7 | } 8 | dependencies { 9 | classpath 'com.github.ksoichiro:gradle-build-info-plugin:+' 10 | } 11 | } 12 | 13 | apply plugin: 'java' 14 | apply plugin: 'com.github.ksoichiro.build.info' 15 | -------------------------------------------------------------------------------- /samples/spring-boot/src/main/java/com/example/App.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | public static void main(String[] args) throws Exception { 9 | SpringApplication.run(App.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: groovy 2 | 3 | jdk: 4 | - openjdk7 5 | - oraclejdk8 6 | 7 | script: 8 | - ./gradlew clean build check uploadArchives --full-stacktrace 9 | - cd samples/example && ./gradlew clean build --full-stacktrace && cd ../../ 10 | - cd samples/spring-boot && ./gradlew clean build --full-stacktrace && cd ../../ 11 | 12 | after_success: 13 | - ./gradlew cobertura coveralls --full-stacktrace 14 | -------------------------------------------------------------------------------- /src/main/groovy/com/github/ksoichiro/build/info/BuildInfoPlugin.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | import org.gradle.api.Plugin 4 | import org.gradle.api.Project 5 | 6 | class BuildInfoPlugin implements Plugin { 7 | @Override 8 | void apply(Project target) { 9 | target.extensions.create(BuildInfoExtension.NAME, BuildInfoExtension) 10 | target.tasks.create(GenerateBuildInfoTask.NAME, GenerateBuildInfoTask) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /samples/spring-boot/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | maven { 5 | url uri('../repo') 6 | } 7 | } 8 | dependencies { 9 | classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RELEASE' 10 | classpath 'com.github.ksoichiro:gradle-build-info-plugin:+' 11 | } 12 | } 13 | 14 | apply plugin: 'com.github.ksoichiro.build.info' 15 | apply plugin: 'java' 16 | apply plugin: 'spring-boot' 17 | 18 | repositories { 19 | mavenCentral() 20 | } 21 | 22 | dependencies { 23 | compile 'org.springframework.boot:spring-boot-starter-web' 24 | compile 'org.springframework.boot:spring-boot-starter-actuator' 25 | } 26 | -------------------------------------------------------------------------------- /gradle/version.gradle: -------------------------------------------------------------------------------- 1 | import org.apache.tools.ant.filters.ReplaceTokens 2 | 3 | def readVariables() { 4 | Properties props = new Properties() 5 | new File("${rootDir}/gradle/variables.properties").withInputStream { 6 | stream -> props.load(stream) 7 | } 8 | props.each { 9 | def name = it.key.replaceAll(/\./, '_').toUpperCase() 10 | if (!ext.has("${name}")) { 11 | ext."${name}" = it.value 12 | } 13 | } 14 | } 15 | readVariables() 16 | 17 | task version << { 18 | println PLUGIN_VERSION 19 | } 20 | 21 | task updateReadme(type: Copy) { 22 | from project.file("README.template.md") 23 | into project.projectDir 24 | rename { "README.md" } 25 | def latestReleaseVersion = PLUGIN_VERSION.replaceAll('-SNAPSHOT', '') 26 | filter(ReplaceTokens, tokens: [PLUGIN_VERSION: latestReleaseVersion]) 27 | } 28 | -------------------------------------------------------------------------------- /gradle/variables.properties: -------------------------------------------------------------------------------- 1 | PLUGIN_VERSION=0.2.0 2 | GROUP=com.github.ksoichiro 3 | POM_ARTIFACT_ID=gradle-build-info-plugin 4 | 5 | POM_NAME=gradle-build-info-plugin 6 | POM_INCEPTION_YEAR=2015 7 | POM_DESCRIPTION=Gradle plugin to include build information to your JAR 8 | POM_URL=https://github.com/ksoichiro/gradle-build-info-plugin 9 | POM_SCM_URL=https://github.com/ksoichiro/gradle-build-info-plugin 10 | POM_SCM_CONNECTION=scm:git@github.com/ksoichiro/gradle-build-info-plugin.git 11 | POM_SCM_DEV_CONNECTION=scm:git@github.com:ksoichiro/gradle-build-info-plugin.git 12 | POM_LICENSE_NAME=Apache Licence 2.0 13 | POM_LICENSE_URL=http://www.apache.org/licenses/LICENSE-2.0 14 | POM_LICENSE_DIST=repo 15 | POM_DEVELOPER_ID=ksoichiro 16 | POM_DEVELOPER_NAME=Soichiro Kashima 17 | POM_DEVELOPER_URL=http://ksoichiro.blogspot.jp/ 18 | 19 | BINTRAY_ISSUE_TRACKER_URL=https://github.com/ksoichiro/gradle-build-info-plugin 20 | BINTRAY_VCS_URL=https://github.com/ksoichiro/gradle-build-info-plugin.git 21 | 22 | GRADLE_PLUGIN_ID=com.github.ksoichiro.build.info 23 | -------------------------------------------------------------------------------- /src/main/groovy/com/github/ksoichiro/build/info/BuildInfoExtension.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | class BuildInfoExtension { 4 | public static final NAME = "buildInfo" 5 | public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z" 6 | 7 | /** 8 | * When the plugin cannot read .git directory, 9 | * set the values of branch, commit, and committer date to "unknown", 10 | * then proceed task. 11 | */ 12 | public static final int MODE_DEFAULT = 0 13 | 14 | /** 15 | * When the plugin cannot read .git directory, 16 | * ignore it and proceed task. 17 | */ 18 | public static final int MODE_IGNORE = 1 19 | 20 | /** 21 | * When the plugin cannot read .git directory, 22 | * throw an exception to stop build. 23 | */ 24 | public static final int MODE_ERROR = 2 25 | 26 | String committerDateFormat = DEFAULT_DATE_FORMAT 27 | String buildDateFormat = DEFAULT_DATE_FORMAT 28 | boolean manifestEnabled = true 29 | boolean gitPropertiesEnabled = false 30 | int gitInfoMode = MODE_DEFAULT 31 | boolean warnIfGitDirectoryIsMissing = true 32 | boolean attributeGitBranchEnabled = true 33 | boolean attributeGitCommitEnabled = true 34 | boolean attributeGitCommitterDateEnabled = true 35 | boolean attributeBuildDateEnabled = true 36 | boolean attributeBuildJavaVersionEnabled = true 37 | boolean attributeBuildJavaVendorEnabled = true 38 | boolean attributeBuildOsNameEnabled = true 39 | boolean attributeBuildOsVersionEnabled = true 40 | File destinationDir 41 | } 42 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /samples/example/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /samples/spring-boot/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/test/groovy/com/github/ksoichiro/build/info/PluginNoGitDirSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | import org.gradle.api.GradleException 4 | import org.gradle.api.Project 5 | import org.gradle.testfixtures.ProjectBuilder 6 | import org.junit.Rule 7 | import org.junit.rules.TemporaryFolder 8 | import spock.lang.Specification 9 | 10 | class PluginNoGitDirSpec extends Specification { 11 | private static final String PLUGIN_ID = 'com.github.ksoichiro.build.info' 12 | 13 | @Rule 14 | public final TemporaryFolder testProjectDir = new TemporaryFolder() 15 | File rootDir 16 | 17 | def setup() { 18 | rootDir = testProjectDir.root 19 | if (!rootDir.exists()) { 20 | rootDir.mkdir() 21 | } 22 | 23 | def pkg = new File("${rootDir}/src/main/java/hello") 24 | pkg.mkdirs() 25 | new File(pkg, "App.java").text = """\ 26 | |package hello; 27 | |public class App { 28 | | public static void main(String[] args) { 29 | | System.out.println("Hello!"); 30 | | } 31 | |} 32 | |""".stripMargin().stripIndent() 33 | } 34 | 35 | def setDefaultStringIfGitInfoDoesNotExist() { 36 | setup: 37 | Project project = ProjectBuilder.builder().build() 38 | project.apply plugin: 'java' 39 | project.apply plugin: PLUGIN_ID 40 | project.buildInfo { 41 | gitPropertiesEnabled true 42 | gitInfoMode BuildInfoExtension.MODE_DEFAULT 43 | } 44 | project.evaluate() 45 | 46 | when: 47 | project.tasks.generateBuildInfo.execute() 48 | 49 | then: 50 | project.file("${project.buildDir}/resources/main/git.properties").exists() 51 | } 52 | 53 | def ignoreIfGitInfoDoesNotExist() { 54 | setup: 55 | Project project = ProjectBuilder.builder().build() 56 | project.apply plugin: 'java' 57 | project.apply plugin: PLUGIN_ID 58 | project.buildInfo { 59 | gitPropertiesEnabled true 60 | gitInfoMode BuildInfoExtension.MODE_IGNORE 61 | } 62 | project.evaluate() 63 | 64 | when: 65 | project.tasks.generateBuildInfo.execute() 66 | 67 | then: 68 | !project.file("${project.buildDir}/resources/main/git.properties").exists() 69 | } 70 | 71 | def throwExceptionIfGitInfoDoesNotExist() { 72 | setup: 73 | Project project = ProjectBuilder.builder().build() 74 | project.apply plugin: 'java' 75 | project.apply plugin: PLUGIN_ID 76 | project.buildInfo { 77 | gitPropertiesEnabled true 78 | gitInfoMode BuildInfoExtension.MODE_ERROR 79 | } 80 | project.evaluate() 81 | 82 | when: 83 | project.tasks.generateBuildInfo.execute() 84 | 85 | then: 86 | thrown(GradleException) 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/test/groovy/com/github/ksoichiro/build/info/MultiProjectFunctionalSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | import org.ajoberstar.grgit.Grgit 4 | import org.gradle.testkit.runner.GradleRunner 5 | import org.gradle.testkit.runner.TaskOutcome 6 | import org.junit.Rule 7 | import org.junit.rules.TemporaryFolder 8 | import spock.lang.Specification 9 | 10 | class MultiProjectFunctionalSpec extends Specification { 11 | private static final String PLUGIN_ID = 'com.github.ksoichiro.build.info' 12 | 13 | @Rule 14 | public final TemporaryFolder testProjectDir = new TemporaryFolder() 15 | File rootDir 16 | File libDir 17 | File appDir 18 | File rootBuildFile 19 | File settingsFile 20 | File libBuildFile 21 | File appBuildFile 22 | List pluginClasspath 23 | Grgit grgit 24 | 25 | def setup() { 26 | rootDir = testProjectDir.root 27 | rootDir.mkdirs() 28 | libDir = new File(rootDir, "library") 29 | libDir.mkdirs() 30 | appDir = new File(rootDir, "app") 31 | appDir.mkdirs() 32 | rootBuildFile = new File(rootDir, "build.gradle") 33 | settingsFile = new File(rootDir, "settings.gradle") 34 | libBuildFile = new File(libDir, "build.gradle") 35 | appBuildFile = new File(appDir, "build.gradle") 36 | 37 | def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.txt") 38 | if (pluginClasspathResource == null) { 39 | throw new IllegalStateException("Did not find plugin classpath resource, run `testClasses` build task.") 40 | } 41 | 42 | pluginClasspath = pluginClasspathResource.readLines() 43 | .collect { it.replace('\\', '\\\\') } // escape backslashes in Windows paths 44 | .collect { new File(it) } 45 | 46 | new File(appDir, ".gitignore").text = """\ 47 | |.gradle/ 48 | |build/ 49 | |""".stripMargin().stripIndent() 50 | def pkg = new File("${appDir}/src/main/java/hello") 51 | pkg.mkdirs() 52 | new File(pkg, "App.java").text = """\ 53 | |package hello; 54 | |public class App { 55 | | public static void main(String[] args) { 56 | | System.out.println("Hello!"); 57 | | } 58 | |} 59 | |""".stripMargin().stripIndent() 60 | 61 | settingsFile.text = """\ 62 | |include ':library', ':app' 63 | |""".stripMargin().stripIndent() 64 | rootBuildFile.text = """\ 65 | |allprojects { 66 | | repositories { 67 | | mavenCentral() 68 | | } 69 | | apply plugin: 'java' 70 | |} 71 | |""".stripMargin().stripIndent() 72 | libBuildFile.text = """\ 73 | |dependencies { 74 | | compile 'org.springframework.boot:spring-boot-starter-actuator:1.3.0.RELEASE' 75 | |} 76 | |""".stripMargin().stripIndent() 77 | appBuildFile.text = """\ 78 | |plugins { 79 | | id '${PLUGIN_ID}' 80 | |} 81 | |dependencies { 82 | | compile project(':library') 83 | |} 84 | |""".stripMargin().stripIndent() 85 | 86 | grgit = Grgit.init(dir: rootDir.path) 87 | grgit.add(patterns: [ 88 | '.gitignore', 89 | 'settings.gradle', 90 | 'library/build.gradle', 91 | 'app/build.gradle', 92 | 'app/src/main/java/hello/App.java']) 93 | grgit.commit(message: 'Initial commit.') 94 | } 95 | 96 | def springBootActuatorInTransitiveDependency() { 97 | when: 98 | def result = GradleRunner.create() 99 | .withProjectDir(rootDir) 100 | .withArguments("build") 101 | .withPluginClasspath(pluginClasspath) 102 | .build() 103 | File propsFile = new File("${appDir}/build/resources/main/git.properties") 104 | 105 | then: 106 | result.task(":build").getOutcome() == TaskOutcome.SUCCESS 107 | propsFile.exists() 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gradle-build-info-plugin 2 | 3 | [![Build Status](https://img.shields.io/travis/ksoichiro/gradle-build-info-plugin/master.svg?style=flat-square)](https://travis-ci.org/ksoichiro/gradle-build-info-plugin) 4 | [![Build status](https://img.shields.io/appveyor/ci/ksoichiro/gradle-build-info-plugin/master.svg?style=flat-square)](https://ci.appveyor.com/project/ksoichiro/gradle-build-info-plugin) 5 | [![Coverage Stagus](https://img.shields.io/coveralls/ksoichiro/gradle-build-info-plugin/master.svg?style=flat-square)](https://coveralls.io/github/ksoichiro/gradle-build-info-plugin?branch=master) 6 | [![Bintray](https://img.shields.io/bintray/v/ksoichiro/maven/gradle-build-info-plugin.svg?style=flat-square)](https://bintray.com/ksoichiro/maven/gradle-build-info-plugin/_latestVersion) 7 | [![Maven Central](http://img.shields.io/maven-central/v/com.github.ksoichiro/gradle-build-info-plugin.svg?style=flat-square)](https://github.com/ksoichiro/gradle-build-info-plugin/releases/latest) 8 | 9 | > Gradle plugin to include build information such as Git commit ID to your JAR. 10 | 11 | ## Usage 12 | 13 | Apply plugin: 14 | 15 | ```gradle 16 | plugins { 17 | id 'com.github.ksoichiro.build.info' version '0.2.0' 18 | } 19 | ``` 20 | 21 | Note that you can use this plugin only when you use java plugin because you build JAR: 22 | 23 | ```gradle 24 | apply plugin: 'java' 25 | ``` 26 | 27 | When used with Spring Boot Actuator dependency, this plugin will also generate 28 | `git.properties`, which will be recognized by Spring Boot Actuator's 29 | info endpoint. 30 | 31 | Then just build your project: 32 | 33 | ```console 34 | $ ./gradlew build 35 | ``` 36 | 37 | Now your Manifest in JAR includes special attributes: 38 | 39 | ```console 40 | $ unzip -p build/libs/yourJar.jar META-INF/Manifest.MF 41 | Manifest-Version: 1.0 42 | Git-Branch: master 43 | Git-Commit: 154f026 44 | Git-Committer-Date: 2015-12-17 20:05:55 +0900 45 | Build-Date: 2015-12-17 20:15:15 +0900 46 | Build-Java-Version: 1.8.0_45 47 | Build-Java-Vendor: Oracle Corporation 48 | Build-Os-Name: Mac OS X 49 | Build-Os-Version: 10.10.1 50 | ``` 51 | 52 | And when Spring Boot Actuator is used with it, git.properties 53 | will be generated: 54 | 55 | ```console 56 | $ cat build/resources/main/git.properties 57 | git.branch=master 58 | git.commit.id=38a0c0c 59 | git.commit.time=2015-12-16 23\:40\:13 +0900 60 | ``` 61 | 62 | ## Configuration 63 | 64 | You can configure this plugin with `buildInfo` extension in build.gradle: 65 | 66 | ```gradle 67 | buildInfo { 68 | // Date format string used to Git committer date. 69 | committerDateFormat 'yyyy-MM-dd HH:mm:ss Z' 70 | 71 | // Date format string used to build date. 72 | buildDateFormat 'yyyy-MM-dd HH:mm:ss Z' 73 | 74 | // Set to true if you want to generate/merge Manifest.MF. 75 | manifestEnabled true 76 | 77 | // Set to true if you want to generate git.properties. 78 | // Default is false, but when you use Spring Boot Actuator 79 | // and you don't set this property explicitly to false, 80 | // git.properties will be generated. 81 | gitPropertiesEnabled false 82 | 83 | // Behavior when the plugin cannot read .git directory. 84 | // Set to MODE_IGNORE if you want to ignore it and proceed task. 85 | // Set to MODE_ERROR if you want to throw an exception to stop build. 86 | // Default is MODE_DEFAULT. 87 | // MODE_DEFAULT will set the values of branch, commit, 88 | // and committer date to "unknown", then proceed task. 89 | gitInfoMode com.github.ksoichiro.build.info.BuildInfoExtension.MODE_DEFAULT 90 | 91 | // Set to false if you want to suppress log when .git directory 92 | // cannot be read. 93 | // Default is true. 94 | warnIfGitDirectoryIsMissing false 95 | 96 | // Set to false if you want to exclude some attributes 97 | // from the manifest file. 98 | // All properties are true by default. 99 | attributeGitBranchEnabled false 100 | attributeGitCommitEnabled false 101 | attributeGitCommitterDateEnabled false 102 | attributeBuildDateEnabled false 103 | attributeBuildJavaVersionEnabled false 104 | attributeBuildJavaVendorEnabled false 105 | attributeBuildOsNameEnabled false 106 | attributeBuildOsVersionEnabled false 107 | } 108 | ``` 109 | 110 | ## License 111 | 112 | Copyright 2015 Soichiro Kashima 113 | 114 | Licensed under the Apache License, Version 2.0 (the "License"); 115 | you may not use this file except in compliance with the License. 116 | You may obtain a copy of the License at 117 | 118 | http://www.apache.org/licenses/LICENSE-2.0 119 | 120 | Unless required by applicable law or agreed to in writing, software 121 | distributed under the License is distributed on an "AS IS" BASIS, 122 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123 | See the License for the specific language governing permissions and 124 | limitations under the License. 125 | -------------------------------------------------------------------------------- /README.template.md: -------------------------------------------------------------------------------- 1 | # gradle-build-info-plugin 2 | 3 | [![Build Status](https://img.shields.io/travis/ksoichiro/gradle-build-info-plugin/master.svg?style=flat-square)](https://travis-ci.org/ksoichiro/gradle-build-info-plugin) 4 | [![Build status](https://img.shields.io/appveyor/ci/ksoichiro/gradle-build-info-plugin/master.svg?style=flat-square)](https://ci.appveyor.com/project/ksoichiro/gradle-build-info-plugin) 5 | [![Coverage Stagus](https://img.shields.io/coveralls/ksoichiro/gradle-build-info-plugin/master.svg?style=flat-square)](https://coveralls.io/github/ksoichiro/gradle-build-info-plugin?branch=master) 6 | [![Bintray](https://img.shields.io/bintray/v/ksoichiro/maven/gradle-build-info-plugin.svg?style=flat-square)](https://bintray.com/ksoichiro/maven/gradle-build-info-plugin/_latestVersion) 7 | [![Maven Central](http://img.shields.io/maven-central/v/com.github.ksoichiro/gradle-build-info-plugin.svg?style=flat-square)](https://github.com/ksoichiro/gradle-build-info-plugin/releases/latest) 8 | 9 | > Gradle plugin to include build information such as Git commit ID to your JAR. 10 | 11 | ## Usage 12 | 13 | Apply plugin: 14 | 15 | ```gradle 16 | plugins { 17 | id 'com.github.ksoichiro.build.info' version '@PLUGIN_VERSION@' 18 | } 19 | ``` 20 | 21 | Note that you can use this plugin only when you use java plugin because you build JAR: 22 | 23 | ```gradle 24 | apply plugin: 'java' 25 | ``` 26 | 27 | When used with Spring Boot Actuator dependency, this plugin will also generate 28 | `git.properties`, which will be recognized by Spring Boot Actuator's 29 | info endpoint. 30 | 31 | Then just build your project: 32 | 33 | ```console 34 | $ ./gradlew build 35 | ``` 36 | 37 | Now your Manifest in JAR includes special attributes: 38 | 39 | ```console 40 | $ unzip -p build/libs/yourJar.jar META-INF/Manifest.MF 41 | Manifest-Version: 1.0 42 | Git-Branch: master 43 | Git-Commit: 154f026 44 | Git-Committer-Date: 2015-12-17 20:05:55 +0900 45 | Build-Date: 2015-12-17 20:15:15 +0900 46 | Build-Java-Version: 1.8.0_45 47 | Build-Java-Vendor: Oracle Corporation 48 | Build-Os-Name: Mac OS X 49 | Build-Os-Version: 10.10.1 50 | ``` 51 | 52 | And when Spring Boot Actuator is used with it, git.properties 53 | will be generated: 54 | 55 | ```console 56 | $ cat build/resources/main/git.properties 57 | git.branch=master 58 | git.commit.id=38a0c0c 59 | git.commit.time=2015-12-16 23\:40\:13 +0900 60 | ``` 61 | 62 | ## Configuration 63 | 64 | You can configure this plugin with `buildInfo` extension in build.gradle: 65 | 66 | ```gradle 67 | buildInfo { 68 | // Date format string used to Git committer date. 69 | committerDateFormat 'yyyy-MM-dd HH:mm:ss Z' 70 | 71 | // Date format string used to build date. 72 | buildDateFormat 'yyyy-MM-dd HH:mm:ss Z' 73 | 74 | // Set to true if you want to generate/merge Manifest.MF. 75 | manifestEnabled true 76 | 77 | // Set to true if you want to generate git.properties. 78 | // Default is false, but when you use Spring Boot Actuator 79 | // and you don't set this property explicitly to false, 80 | // git.properties will be generated. 81 | gitPropertiesEnabled false 82 | 83 | // Behavior when the plugin cannot read .git directory. 84 | // Set to MODE_IGNORE if you want to ignore it and proceed task. 85 | // Set to MODE_ERROR if you want to throw an exception to stop build. 86 | // Default is MODE_DEFAULT. 87 | // MODE_DEFAULT will set the values of branch, commit, 88 | // and committer date to "unknown", then proceed task. 89 | gitInfoMode com.github.ksoichiro.build.info.BuildInfoExtension.MODE_DEFAULT 90 | 91 | // Set to false if you want to suppress log when .git directory 92 | // cannot be read. 93 | // Default is true. 94 | warnIfGitDirectoryIsMissing false 95 | 96 | // Set to false if you want to exclude some attributes 97 | // from the manifest file. 98 | // All properties are true by default. 99 | attributeGitBranchEnabled false 100 | attributeGitCommitEnabled false 101 | attributeGitCommitterDateEnabled false 102 | attributeBuildDateEnabled false 103 | attributeBuildJavaVersionEnabled false 104 | attributeBuildJavaVendorEnabled false 105 | attributeBuildOsNameEnabled false 106 | attributeBuildOsVersionEnabled false 107 | } 108 | ``` 109 | 110 | ## License 111 | 112 | Copyright 2015 Soichiro Kashima 113 | 114 | Licensed under the Apache License, Version 2.0 (the "License"); 115 | you may not use this file except in compliance with the License. 116 | You may obtain a copy of the License at 117 | 118 | http://www.apache.org/licenses/LICENSE-2.0 119 | 120 | Unless required by applicable law or agreed to in writing, software 121 | distributed under the License is distributed on an "AS IS" BASIS, 122 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123 | See the License for the specific language governing permissions and 124 | limitations under the License. 125 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /samples/example/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /samples/spring-boot/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /src/test/groovy/com/github/ksoichiro/build/info/PluginSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | import org.ajoberstar.grgit.Grgit 4 | import org.gradle.api.Project 5 | import org.gradle.api.ProjectConfigurationException 6 | import org.gradle.testfixtures.ProjectBuilder 7 | import org.junit.Rule 8 | import org.junit.rules.TemporaryFolder 9 | import spock.lang.Specification 10 | 11 | class PluginSpec extends Specification { 12 | private static final String PLUGIN_ID = 'com.github.ksoichiro.build.info' 13 | 14 | @Rule 15 | public final TemporaryFolder testProjectDir = new TemporaryFolder() 16 | 17 | File rootDir 18 | Grgit grgit 19 | 20 | def setup() { 21 | rootDir = testProjectDir.root 22 | if (!rootDir.exists()) { 23 | rootDir.mkdir() 24 | } 25 | 26 | new File(rootDir, ".gitignore").text = """\ 27 | |.gradle/ 28 | |/build/ 29 | |""".stripMargin().stripIndent() 30 | def pkg = new File("${rootDir}/src/main/java/hello") 31 | pkg.mkdirs() 32 | new File(pkg, "App.java").text = """\ 33 | |package hello; 34 | |public class App { 35 | | public static void main(String[] args) { 36 | | System.out.println("Hello!"); 37 | | } 38 | |} 39 | |""".stripMargin().stripIndent() 40 | grgit = Grgit.init(dir: rootDir.path) 41 | grgit.add(patterns: ['.gitignore', 'build.gradle', 'src/main/java/hello/App.java']) 42 | grgit.commit(message: 'Initial commit.') 43 | } 44 | 45 | def apply() { 46 | setup: 47 | Project project = ProjectBuilder.builder().build() 48 | 49 | when: 50 | project.apply plugin: PLUGIN_ID 51 | 52 | then: 53 | project.tasks.generateBuildInfo instanceof GenerateBuildInfoTask 54 | } 55 | 56 | def generateWithJavaPlugin() { 57 | setup: 58 | Project project = ProjectBuilder.builder().withProjectDir(rootDir).build() 59 | project.apply plugin: 'java' 60 | project.apply plugin: PLUGIN_ID 61 | project.evaluate() 62 | 63 | when: 64 | project.tasks.generateBuildInfo.execute() 65 | 66 | then: 67 | !project.file("${project.buildDir}/resources/main/git.properties").exists() 68 | } 69 | 70 | def generateWithJavaPluginAndEnabled() { 71 | setup: 72 | Project project = ProjectBuilder.builder().withProjectDir(rootDir).build() 73 | project.apply plugin: 'java' 74 | project.apply plugin: PLUGIN_ID 75 | project.buildInfo { 76 | gitPropertiesEnabled true 77 | } 78 | project.evaluate() 79 | 80 | when: 81 | project.tasks.generateBuildInfo.execute() 82 | 83 | then: 84 | project.file("${project.buildDir}/resources/main/git.properties").exists() 85 | } 86 | 87 | def generateWithoutJavaPlugin() { 88 | setup: 89 | Project project = ProjectBuilder.builder().withProjectDir(rootDir).build() 90 | project.apply plugin: PLUGIN_ID 91 | 92 | when: 93 | project.evaluate() 94 | 95 | then: 96 | thrown(ProjectConfigurationException) 97 | } 98 | 99 | def generateWithoutJavaPluginAndEnabledAndSetAnotherDestination() { 100 | setup: 101 | Project project = ProjectBuilder.builder().withProjectDir(rootDir).build() 102 | project.apply plugin: PLUGIN_ID 103 | project.buildInfo { 104 | gitPropertiesEnabled true 105 | destinationDir project.file("${project.buildDir}/foo/bar/") 106 | } 107 | project.evaluate() 108 | 109 | when: 110 | project.tasks.generateBuildInfo.execute() 111 | 112 | then: 113 | !project.file("${project.buildDir}/resources/main/git.properties").exists() 114 | project.file("${project.buildDir}/foo/bar/git.properties").exists() 115 | } 116 | 117 | def generateWithJavaPluginAndSpringBootActuator() { 118 | setup: 119 | Project project = ProjectBuilder.builder().withProjectDir(rootDir).build() 120 | project.apply plugin: 'java' 121 | project.apply plugin: PLUGIN_ID 122 | project.repositories { 123 | mavenCentral() 124 | } 125 | project.dependencies { 126 | compile 'org.springframework.boot:spring-boot-starter-actuator:1.3.0.RELEASE' 127 | } 128 | project.evaluate() 129 | 130 | when: 131 | project.tasks.generateBuildInfo.execute() 132 | 133 | then: 134 | project.file("${project.buildDir}/resources/main/git.properties").exists() 135 | } 136 | 137 | def configureExtension() { 138 | setup: 139 | Project project = ProjectBuilder.builder().build() 140 | project.apply plugin: 'java' 141 | project.apply plugin: PLUGIN_ID 142 | project.buildInfo { 143 | buildDateFormat 'yyyy-MM-dd' 144 | committerDateFormat 'yyyy-MM-dd' 145 | } 146 | project.evaluate() 147 | 148 | when: 149 | project.tasks.generateBuildInfo.execute() 150 | 151 | then: 152 | !project.file("${project.buildDir}/resources/main/git.properties").exists() 153 | } 154 | 155 | def transitiveDependency() { 156 | setup: 157 | Project project = ProjectBuilder.builder().build() 158 | project.apply plugin: 'java' 159 | project.apply plugin: PLUGIN_ID 160 | project.repositories { 161 | mavenCentral() 162 | } 163 | project.dependencies { 164 | compile 'org.springframework.boot:spring-boot-starter-actuator:1.3.0.RELEASE' 165 | } 166 | 167 | when: 168 | project.evaluate() 169 | 170 | then: 171 | GenerateBuildInfoTask.hasDependency(project, 'org.springframework', 'spring-core') 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /src/main/groovy/com/github/ksoichiro/build/info/GenerateBuildInfoTask.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | import org.ajoberstar.grgit.Commit 4 | import org.ajoberstar.grgit.Grgit 5 | import org.gradle.api.DefaultTask 6 | import org.gradle.api.GradleException 7 | import org.gradle.api.Project 8 | import org.gradle.api.Task 9 | import org.gradle.api.artifacts.component.ModuleComponentIdentifier 10 | import org.gradle.api.plugins.JavaPlugin 11 | import org.gradle.api.tasks.StopExecutionException 12 | import org.gradle.api.tasks.TaskAction 13 | import org.gradle.api.tasks.bundling.Jar 14 | import org.gradle.language.jvm.tasks.ProcessResources 15 | 16 | class GenerateBuildInfoTask extends DefaultTask { 17 | public static final String NAME = 'generateBuildInfo' 18 | BuildInfoExtension extension 19 | GitInfo gitInfo 20 | File propsFile 21 | boolean hasDependency 22 | 23 | GenerateBuildInfoTask() { 24 | project.afterEvaluate { 25 | extension = project.extensions.buildInfo 26 | if (project.plugins.hasPlugin(JavaPlugin)) { 27 | dependsOn(JavaPlugin.PROCESS_RESOURCES_TASK_NAME) 28 | getClassesTask().dependsOn(NAME) 29 | } 30 | 31 | // Should detect... 32 | // changes of git commit ids: input=commit-id, output=file 33 | // changes of existence of .git directory: input=existence-flag, output=file 34 | // Should ignore... 35 | // changes when git status is dirty: input=commit-id, output=file 36 | gitInfo = readGitInfo() 37 | File buildResourcesDir = getBuildResourcesDir() 38 | propsFile = new File(buildResourcesDir, 'git.properties') 39 | getInputs().property('valid', gitInfo.valid) 40 | getInputs().property('commit', gitInfo.commit) 41 | getOutputs().file(propsFile) 42 | } 43 | } 44 | 45 | @TaskAction 46 | void exec() { 47 | hasDependency = hasDependency(project, 'org.springframework.boot', 'spring-boot-starter-actuator') 48 | validate() 49 | generateGitProperties() 50 | mergeManifest() 51 | } 52 | 53 | Task getClassesTask() { 54 | project.tasks.findByName(JavaPlugin.CLASSES_TASK_NAME) 55 | } 56 | 57 | File getBuildResourcesDir() { 58 | ProcessResources processResources = getProcessResourcesTask() 59 | if (processResources) { 60 | return processResources.destinationDir 61 | } else if (extension.destinationDir) { 62 | return extension.destinationDir 63 | } else { 64 | // Cannot determine destination 65 | throw new GradleException("Could not determine destination directory. You must apply Java plugin or set buildInfo.destinationDir.") 66 | } 67 | } 68 | 69 | ProcessResources getProcessResourcesTask() { 70 | (ProcessResources) project.tasks.findByName(JavaPlugin.PROCESS_RESOURCES_TASK_NAME) 71 | } 72 | 73 | void generateGitProperties() { 74 | if (!shouldGenerateGitProperties()) { 75 | return 76 | } 77 | if (gitInfo.missing && extension.warnIfGitDirectoryIsMissing) { 78 | logger.warn "Could not read .git directory. git.properties will not be generated or will include invalid values." 79 | } 80 | if (!gitInfo.valid) { 81 | return 82 | } 83 | if (!propsFile.parentFile.exists()) { 84 | propsFile.parentFile.mkdirs() 85 | } 86 | def map = [ 87 | 'git.branch' : gitInfo.branch, 88 | 'git.commit.id' : gitInfo.commit, 89 | 'git.commit.time': gitInfo.committerDate] 90 | 91 | // Content of git.properties should always be the same 92 | // if it's generated for the same commit. 93 | // However, Properties class uses Hashtable internally and output timestamp, 94 | // which produces inconsistent content for the same commit. 95 | // To avoid this, we can just write file by ourselves, but Properties also 96 | // has "escape" feature and it's provided as private method. 97 | // With Groovy, private methods can be accessed from outside of a class, 98 | // but it's not a feature but just an unresolved issue, 99 | // so this behaviour might be changed in the future. 100 | // https://issues.apache.org/jira/browse/GROOVY-1875 101 | // Therefore we write the properties file with Properties class first 102 | // to create "valid" .properties file, then read it, manipulate it, and save it again. 103 | def props = new Properties() 104 | props.putAll(map) 105 | props.store(propsFile.newWriter(), null) 106 | def buffer = [] 107 | propsFile.eachLine { 108 | // Timestamp line should be discarded 109 | if (!it.startsWith("#")) { 110 | buffer << it 111 | } 112 | } 113 | buffer.sort(true) 114 | propsFile.withWriter { w -> 115 | buffer.each { 116 | w.println it 117 | } 118 | } 119 | } 120 | 121 | void mergeManifest() { 122 | if (!shouldMergeManifest()) { 123 | return 124 | } 125 | 126 | def attributes = [:] as Map 127 | 128 | extension.with { 129 | if (gitInfo.missing && extension.warnIfGitDirectoryIsMissing) { 130 | logger.warn "Could not read .git directory. Git info will not be included in the manifest or will be replaced to invalid values." 131 | } 132 | if (gitInfo.valid) { 133 | if (attributeGitBranchEnabled) { 134 | attributes["Git-Branch"] = gitInfo.branch 135 | } 136 | if (attributeGitCommitEnabled) { 137 | attributes["Git-Commit"] = gitInfo.commit 138 | } 139 | if (attributeGitCommitterDateEnabled) { 140 | attributes["Git-Committer-Date"] = gitInfo.committerDate 141 | } 142 | } 143 | if (attributeBuildDateEnabled) { 144 | attributes["Build-Date"] = new Date().format(extension.buildDateFormat) 145 | } 146 | if (attributeBuildJavaVersionEnabled) { 147 | attributes["Build-Java-Version"] = System.properties['java.version'] 148 | } 149 | if (attributeBuildJavaVendorEnabled) { 150 | attributes["Build-Java-Vendor"] = System.properties['java.vendor'] 151 | } 152 | if (attributeBuildOsNameEnabled) { 153 | attributes["Build-Os-Name"] = System.properties['os.name'] 154 | } 155 | if (attributeBuildOsVersionEnabled) { 156 | attributes["Build-Os-Version"] = System.properties['os.version'] 157 | } 158 | } 159 | 160 | (project.tasks.jar as Jar).manifest { 161 | it.attributes(attributes) 162 | } 163 | } 164 | 165 | GitInfo readGitInfo() { 166 | def missing = false 167 | def valid = true 168 | def branch 169 | def commit 170 | def committerDate 171 | try { 172 | Grgit grgit = Grgit.open(currentDir: project.projectDir) 173 | branch = grgit.branch.current.name 174 | Commit head = grgit.head() 175 | commit = head.abbreviatedId 176 | committerDate = head.date.format(extension.committerDateFormat) 177 | } catch (ignored) { 178 | missing = true 179 | // When MODE_IGNORE is used, we skip outputting related info 180 | valid = extension.gitInfoMode != BuildInfoExtension.MODE_IGNORE 181 | branch = "unknown" 182 | commit = "unknown" 183 | committerDate = "unknown" 184 | } 185 | new GitInfo(missing: missing, 186 | valid: valid, 187 | branch: branch, 188 | commit: commit, 189 | committerDate: committerDate) 190 | } 191 | 192 | def validate() { 193 | // If all options are disabled, skip this task not to cache the result 194 | if (!shouldGenerateGitProperties() && !shouldMergeManifest()) { 195 | throw new StopExecutionException() 196 | } 197 | if (gitInfo.missing) { 198 | switch (extension.gitInfoMode) { 199 | case BuildInfoExtension.MODE_ERROR: 200 | throw new GradleException("Cannot read .git directory.") 201 | case BuildInfoExtension.MODE_IGNORE: 202 | case BuildInfoExtension.MODE_DEFAULT: 203 | default: 204 | break 205 | } 206 | } 207 | } 208 | 209 | boolean shouldGenerateGitProperties() { 210 | extension.gitPropertiesEnabled || hasDependency 211 | } 212 | 213 | boolean shouldMergeManifest() { 214 | project.plugins.hasPlugin(JavaPlugin) && extension.manifestEnabled 215 | } 216 | 217 | static boolean hasDependency(Project project, String group, String name) { 218 | if (!project.plugins.hasPlugin(JavaPlugin)) { 219 | return false 220 | } 221 | project.configurations.compile.dependencies.any { 222 | it.group == group && it.name == name 223 | } || project.configurations.compile.incoming.resolutionResult.allComponents.findAll { 224 | it.getId() instanceof ModuleComponentIdentifier 225 | }.collect { 226 | it.getId() as ModuleComponentIdentifier 227 | }.any { 228 | it.group == group && it.module == name 229 | } 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /src/test/groovy/com/github/ksoichiro/build/info/FunctionalSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.github.ksoichiro.build.info 2 | 3 | import org.ajoberstar.grgit.Grgit 4 | import org.gradle.testkit.runner.BuildResult 5 | import org.gradle.testkit.runner.GradleRunner 6 | import org.gradle.testkit.runner.TaskOutcome 7 | import org.junit.Rule 8 | import org.junit.rules.TemporaryFolder 9 | import spock.lang.Specification 10 | 11 | import java.util.jar.JarFile 12 | 13 | class FunctionalSpec extends Specification { 14 | private static final String PLUGIN_ID = 'com.github.ksoichiro.build.info' 15 | 16 | @Rule 17 | public final TemporaryFolder testProjectDir = new TemporaryFolder() 18 | File rootDir 19 | File buildFile 20 | List pluginClasspath 21 | Grgit grgit 22 | 23 | def setup() { 24 | rootDir = testProjectDir.root 25 | if (!rootDir.exists()) { 26 | rootDir.mkdir() 27 | } 28 | buildFile = new File(rootDir, "build.gradle") 29 | 30 | def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.txt") 31 | if (pluginClasspathResource == null) { 32 | throw new IllegalStateException("Did not find plugin classpath resource, run `testClasses` build task.") 33 | } 34 | 35 | pluginClasspath = pluginClasspathResource.readLines() 36 | .collect { it.replace('\\', '\\\\') } // escape backslashes in Windows paths 37 | .collect { new File(it) } 38 | 39 | new File(rootDir, ".gitignore").text = """\ 40 | |.gradle/ 41 | |/build/ 42 | |""".stripMargin().stripIndent() 43 | def pkg = new File("${rootDir}/src/main/java/hello") 44 | pkg.mkdirs() 45 | new File(pkg, "App.java").text = """\ 46 | |package hello; 47 | |public class App { 48 | | public static void main(String[] args) { 49 | | System.out.println("Hello!"); 50 | | } 51 | |} 52 | |""".stripMargin().stripIndent() 53 | grgit = Grgit.init(dir: rootDir.path) 54 | grgit.add(patterns: ['.gitignore', 'build.gradle', 'src/main/java/hello/App.java']) 55 | grgit.commit(message: 'Initial commit.') 56 | } 57 | 58 | def generateBuildInfo() { 59 | setup: 60 | def buildFileContent = """\ 61 | |plugins { 62 | | id '${PLUGIN_ID}' 63 | |} 64 | |apply plugin: 'java' 65 | |archivesBaseName = 'foo' 66 | |""".stripMargin().stripIndent() 67 | buildFile.text = buildFileContent 68 | 69 | when: 70 | def result = runBuild() 71 | 72 | then: 73 | result.task(":build").getOutcome() == TaskOutcome.SUCCESS 74 | File jarFile = new File("${rootDir}/build/libs/foo.jar") 75 | jarFile.exists() 76 | new JarFile(jarFile).manifest.mainAttributes.getValue('Git-Commit') == grgit.head().abbreviatedId 77 | } 78 | 79 | def gitPropertiesEnabled() { 80 | setup: 81 | def buildFileContent = """\ 82 | |plugins { 83 | | id '${PLUGIN_ID}' 84 | |} 85 | |apply plugin: 'java' 86 | |buildInfo { 87 | | gitPropertiesEnabled true 88 | |} 89 | |""".stripMargin().stripIndent() 90 | buildFile.text = buildFileContent 91 | 92 | when: 93 | def result = runBuild() 94 | File propsFile = new File("${rootDir}/build/resources/main/git.properties") 95 | 96 | then: 97 | result.task(":build").getOutcome() == TaskOutcome.SUCCESS 98 | propsFile.exists() 99 | 100 | when: 101 | Properties props = new Properties() 102 | props.load(propsFile.newReader()) 103 | 104 | then: 105 | grgit.branch.current.name == props.get('git.branch') 106 | grgit.head().abbreviatedId == props.get('git.commit.id') 107 | grgit.head().date.format('yyyy-MM-dd HH:mm:ss Z') == props.get('git.commit.time') 108 | } 109 | 110 | def gitPropertiesDisabled() { 111 | setup: 112 | def buildFileContent = """\ 113 | |plugins { 114 | | id '${PLUGIN_ID}' 115 | |} 116 | |apply plugin: 'java' 117 | |buildInfo { 118 | | gitPropertiesEnabled false 119 | |} 120 | |""".stripMargin().stripIndent() 121 | buildFile.text = buildFileContent 122 | 123 | when: 124 | def result = runBuild() 125 | 126 | then: 127 | result.task(":build").getOutcome() == TaskOutcome.SUCCESS 128 | 129 | File propsFile = new File("${rootDir}/build/resources/main/git.properties") 130 | !propsFile.exists() 131 | } 132 | 133 | def gitPropertiesDisabledAsDefault() { 134 | setup: 135 | def buildFileContent = """\ 136 | |plugins { 137 | | id '${PLUGIN_ID}' 138 | |} 139 | |apply plugin: 'java' 140 | |""".stripMargin().stripIndent() 141 | buildFile.text = buildFileContent 142 | 143 | when: 144 | def result = runBuild() 145 | 146 | then: 147 | result.task(":build").getOutcome() == TaskOutcome.SUCCESS 148 | 149 | File propsFile = new File("${rootDir}/build/resources/main/git.properties") 150 | !propsFile.exists() 151 | } 152 | 153 | def gitPropertiesEnabledWhenSpringBootActuatorIsUsed() { 154 | setup: 155 | def buildFileContent = """\ 156 | |plugins { 157 | | id '${PLUGIN_ID}' 158 | |} 159 | |apply plugin: 'java' 160 | |repositories { 161 | | mavenCentral() 162 | |} 163 | |dependencies { 164 | | compile 'org.springframework.boot:spring-boot-starter-actuator:1.3.0.RELEASE' 165 | |} 166 | |""".stripMargin().stripIndent() 167 | buildFile.text = buildFileContent 168 | 169 | when: 170 | def result = runBuild() 171 | 172 | then: 173 | result.task(":build").getOutcome() == TaskOutcome.SUCCESS 174 | 175 | File propsFile = new File("${rootDir}/build/resources/main/git.properties") 176 | propsFile.exists() 177 | } 178 | 179 | def manifestDisabled() { 180 | setup: 181 | def buildFileContent = """\ 182 | |plugins { 183 | | id '${PLUGIN_ID}' 184 | |} 185 | |apply plugin: 'java' 186 | |archivesBaseName = 'foo' 187 | |buildInfo { 188 | | manifestEnabled false 189 | |} 190 | |""".stripMargin().stripIndent() 191 | buildFile.text = buildFileContent 192 | 193 | when: 194 | def result = runBuild() 195 | 196 | then: 197 | result.task(":build").getOutcome() == TaskOutcome.SUCCESS 198 | 199 | File jarFile = new File("${rootDir}/build/libs/foo.jar") 200 | jarFile.exists() 201 | JarFile jar = new JarFile(jarFile) 202 | def manifestAttrs = jar.manifest.mainAttributes 203 | !manifestAttrs.containsKey('Git-Branch') 204 | !manifestAttrs.containsKey('Git-Commit') 205 | !manifestAttrs.containsKey('Git-Committer-Date') 206 | !manifestAttrs.containsKey('Build-Date') 207 | !manifestAttrs.containsKey('Build-Java-Version') 208 | !manifestAttrs.containsKey('Build-Java-Vendor') 209 | !manifestAttrs.containsKey('Build-Os-Name') 210 | !manifestAttrs.containsKey('Build-Os-Version') 211 | } 212 | 213 | def upToDateWhenCommitIdDoesNotChange() { 214 | setup: 215 | def buildFileContent = """\ 216 | |plugins { 217 | | id '${PLUGIN_ID}' 218 | |} 219 | |apply plugin: 'java' 220 | |buildInfo { 221 | | gitPropertiesEnabled true 222 | |} 223 | |""".stripMargin().stripIndent() 224 | buildFile.text = buildFileContent 225 | 226 | when: 227 | def result = run('1st') 228 | 229 | then: 230 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.SUCCESS 231 | 232 | when: 233 | result = run('2nd') 234 | 235 | then: 236 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.UP_TO_DATE 237 | 238 | when: 239 | // Changing output causes build (removing file does not work on Windows...) 240 | new File("${rootDir}/build/resources/main/git.properties").canonicalFile.text += "\ntest" 241 | result = run('3rd') 242 | 243 | then: 244 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.SUCCESS 245 | } 246 | 247 | def doNotSkipWhenCommitIdChanges() { 248 | setup: 249 | def buildFileContent = """\ 250 | |plugins { 251 | | id '${PLUGIN_ID}' 252 | |} 253 | |apply plugin: 'java' 254 | |buildInfo { 255 | | gitPropertiesEnabled true 256 | |} 257 | |""".stripMargin().stripIndent() 258 | buildFile.text = buildFileContent 259 | 260 | when: 261 | def result = run('1st') 262 | 263 | then: 264 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.SUCCESS 265 | 266 | when: 267 | new File(rootDir, "README.md").text = """\ 268 | |# Test 269 | |""".stripMargin().stripIndent() 270 | grgit.add(patterns: ['README.md']) 271 | grgit.commit(message: 'Add readme') 272 | result = run('2nd') 273 | 274 | then: 275 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.SUCCESS 276 | } 277 | 278 | def skipWhenAllOfTheFeaturesAreDisabled() { 279 | setup: 280 | def buildFileContent = """\ 281 | |plugins { 282 | | id '${PLUGIN_ID}' 283 | |} 284 | |apply plugin: 'java' 285 | |buildInfo { 286 | | gitPropertiesEnabled false 287 | | manifestEnabled false 288 | |} 289 | |""".stripMargin().stripIndent() 290 | buildFile.text = buildFileContent 291 | 292 | when: 293 | def result = run('1st') 294 | 295 | then: 296 | // Throwing StopExecutionException results in SUCCESS (not SKIPPED or UP_TO_DATE) 297 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.SUCCESS 298 | } 299 | 300 | def skipWhenGitStatusIsDirtyAndSomethingChanges() { 301 | setup: 302 | def buildFileContent = """\ 303 | |plugins { 304 | | id '${PLUGIN_ID}' 305 | |} 306 | |apply plugin: 'java' 307 | |buildInfo { 308 | | gitPropertiesEnabled true 309 | |} 310 | |""".stripMargin().stripIndent() 311 | buildFile.text = buildFileContent 312 | 313 | when: 314 | // Make working copy dirty 315 | new File(rootDir, "README.md").text = """\ 316 | |# Test 317 | |""".stripMargin().stripIndent() 318 | 319 | def result = run('1st') 320 | 321 | then: 322 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.SUCCESS 323 | 324 | when: 325 | // Update dirty working copy 326 | new File(rootDir, "README.md").text = """\ 327 | |# Test 328 | | 329 | |This is a test. 330 | |""".stripMargin().stripIndent() 331 | 332 | result = run('2nd') 333 | 334 | then: 335 | // Even when some files are changed, it is assumed to be up-to-date because the commit does not change 336 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.UP_TO_DATE 337 | 338 | when: 339 | // Commit causes build 340 | grgit.add(patterns: ['README.md']) 341 | grgit.commit(message: 'Add readme') 342 | result = run('3rd') 343 | 344 | then: 345 | // Even when some files are changed, it is assumed to be up-to-date because the commit does not change 346 | result.task(":${GenerateBuildInfoTask.NAME}").getOutcome() == TaskOutcome.SUCCESS 347 | } 348 | 349 | BuildResult runBuild() { 350 | GradleRunner.create() 351 | .withProjectDir(rootDir) 352 | .withArguments('build') 353 | .withPluginClasspath(pluginClasspath) 354 | .build() 355 | } 356 | 357 | BuildResult run(def label) { 358 | BuildResult result = GradleRunner.create() 359 | .withProjectDir(rootDir) 360 | .withArguments(GenerateBuildInfoTask.NAME) 361 | .withPluginClasspath(pluginClasspath) 362 | .build() 363 | 364 | if (label) { 365 | println label 366 | } 367 | File props = new File("${rootDir}/build/resources/main/git.properties") 368 | if (props.exists()) { 369 | println props.text 370 | } 371 | result 372 | } 373 | } 374 | --------------------------------------------------------------------------------