在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):mfuerstenau/gradle-buildconfig-plugin开源软件地址(OpenSource Url):https://github.com/mfuerstenau/gradle-buildconfig-plugin开源编程语言(OpenSource Language):Groovy 100.0%开源软件介绍(OpenSource Introduction):BuildConfig Gradle-plugin for Java and Groovy projectsIncludes a class with constants accessible at runtime set by the build script process. The constants can be defined using a closure DependencyThere are many ways to include the dependency. Easy (Gradle 2.1+)plugins {
id 'de.fuerstenau.buildconfig' version '1.1.8'
} Classic (Gradle prior to 2.1)A little bit more control though. buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'gradle.plugin.de.fuerstenau:BuildConfigPlugin:1.1.8'
}
}
apply plugin: 'de.fuerstenau.buildconfig' // actually applies the plugin LocalYou can download the buildscript {
dependencies {
classpath files ("${projectDir}/lib/BuildConfig-1.1.8-SNAPSHOT.jar") // insert the path to .jar-file
}
}
apply plugin: 'de.fuerstenau.buildconfig' By default a IDEsThere are requirements for different IDEs. NetbeansAs of Netbeans 8.1 with gradle plugin there is no further requirement. Works out of the box. Intellij IDEATested with IntelliJ IDEA 2016. Generation works out of the box. The generated classes are only resolved when the IDEA gradle plugin is applied and after compiling. apply plugin: 'idea' EclipseEclipse (Mars) with the buildship plugin resolves the generated classes after a project refresh. Also the Eclipse gradle plugin needs to be applied. apply plugin: 'eclipse' BuildConfigA
A Here is an example how such a generated Java class might look like: package de.fuerstenau.buildconfig;
/** DO NOT EDIT. GENERATED CODE */
public final class BuildConfig
{
private BuildConfig () { /*. no instance */ }
public static final String VERSION = "1.0-SNAPSHOT";
public static final String NAME = "HelloWorld";
} and usage inside the application might look like this: package some.package;
import de.fuerstenau.buildconfig.BuildConfig;
public class HelloBuildConfig
{
public static void main (String[] args)
{
System.out.println (BuildConfig.NAME);
System.out.println (BuildConfig.VERSION);
}
} The generated Java code won't normally show up because the compiled class will be added to the classpath and it really depends on the IDE used, if the class is visible somewhere (Netbeans 8.1 shows it after building reloading a project as dependency), see IDE section. ConfigurationBasic configurationThe plugin can be configured using the provided buildConfig {
appName = project.name // sets value of NAME field
version = project.version // sets value of VERSION field,
// 'unspecified' if project.version is not set
clsName = 'BuildConfig' // sets the name of the BuildConfig class
packageName = project.group // sets the package of the BuildConfig class,
// 'de.fuerstenau.buildconfig' if project.group is not set
charset = 'UTF-8' // sets charset of the generated class,
// 'UTF-8' if not set otherwise
} Additional fieldsAdditional fields can be added to the
Note: Instead of an explicit value a Note: No Example: buildConfig {
buildConfigField 'String', 'MY_STR_FIELD', '"my message to the app"'
buildConfigField 'String', 'MY_STR_FIELD2', {
'some lazy evaluated value'
}
buildConfigField 'int', 'MY_INT_FIELD', '42'
buildConfigField 'byte[]', 'MY_BYTE_ARRAY_FIELD', '{ (byte) 0xfa, (byte) 0x20, (byte) 0x22 }'
buildConfigField 'long', 'BUILD_UNIXTIME', System.currentTimeMillis() + 'L'
buildConfigField 'java.util.Date', 'BUILD_DATE', 'new java.util.Date(' + System.currentTimeMillis() + 'L)'
buildConfigField 'java.time.Instant', 'BUILD_INSTANT', 'java.time.Instant.ofEpochMilli(' + System.currentTimeMillis() + 'L)'
} Note: There is some black magic included to make buildConfigField 'String', 'MY_STR_FIELD', '"my message to the app"'
buildConfigField 'char', 'MY_CHAR_FIELD', "'x'" // or '\'x\'' for that matter one could alsow write buildConfigField 'String', 'MY_STR_FIELD', 'my message to the app'
buildConfigField 'char', 'MY_CHAR_FIELD', 'x' Per-SourceSet-ConfigurationIt's possible to configure per buildConfig {
sourceSets {
main {
// configuration of 'main' SourceSet
}
someOther {
// configuration of 'someOther' SourceSet
}
}
} There is some special behaviour though:
The following configuration omits a buildConfig {
appName = 'MyAppName'
sourceSets {
someOther {
appName = 'MyOtherAppName'
// configuration of 'someOther' SourceSet
}
yetSomeOther {
// configuration of 'yetSomeOther' SourceSet
}
}
} Advanced usageManual task wiringThis is an example of manually creating the tasks and wiring them to generate and compile a build config for First we don't want the plugin to be applied, only to be resolved. This can be done like this Prior to Gradle 3.0buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'gradle.plugin.de.fuerstenau:BuildConfigPlugin:1.1.8'
}
}
plugins {
id 'java'
} Note: we don't apply the plugin, we just need to resolve the classpath. Gradle 3.0+Since Gradle 3.0 there is a new option to resolve a plugin and make it available on the classpath, but not to apply it. That way You can omit the whole plugins {
id 'java'
id 'de.fuerstenau.buildconfig' version '1.1.8' apply false
} Note: The apply false at the end of the plugin declaration. And then we create the tasks and wire them /* the generating task, creates the .java-file */
task generateBuildConfig (type: de.fuerstenau.buildconfig.GenerateBuildConfigTask) {
/* we need to define an output dir for the generated .java-file */
outputDir = new File ("${buildDir}/gen/buildconfig/src/main/")
/* the task has nearly the same properties as the buildconfig closure */
appName = 'SuperTrooperStarshipApp'
clsName = 'MainConfig'
packageName = 'org.sample'
buildConfigField 'int', 'MY_INT_FIELD', '42'
}
/* the compiling task, compiles the generated .java file */
task compileBuildConfig(type:JavaCompile, dependsOn: generateBuildConfig) {
classpath = files () // we need no extra class path
/* where do we want our .class file */
destinationDir = new File ("${buildDir}/gen/buildconfig/classes/main/")
/* the input is the output of the generating task */
source = generateBuildConfig.outputDir
}
sourceSets {
main {
/* last but not least we want our buildconfig to be part of the classpath */
compileClasspath += compileBuildConfig.outputs.files
/* also we want the .class-file to be included in the default .jar-artifact,
* therefore we add our outputs to the sourceset's outputs, we nee to filter out
* the dependency-cache */
compileBuildConfig.outputs.files.findAll {
!it.name.endsWith ('dependency-cache') // we don't want these
}.each {
output.dir it // add everything else
}
}
} Alternative Maven repositoryShould there be a problem with the repository at buildscript {
repositories {
maven {
url 'https://dl.bintray.com/mfuerstenau/maven'
}
}
dependencies {
classpath 'gradle.plugin.de.fuerstenau:BuildConfigPlugin:1.1.8'
}
}
apply plugin: 'de.fuerstenau.buildconfig' // actually applies the plugin Compatibility
Gradle plugin portalThe plugin is listed at https://plugins.gradle.org/plugin/de.fuerstenau.buildconfig. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论