みけです。
Gradleをいつも推していますが、
build.gradleがいつも同じ事ばっかり書いている気がしたので、
gradle builder 略して、gradlebを書いてみました。
aliasに
alias gradleb = 'groovy /path/to/gradleb.groovy'
と設定しておいて、下のように叩くと、プロジェクト構造とbuild.gradleが生成されます。
$ gradleb -g org.mikeneck.gradleb.sample -j 1.7 -p sample-web -v 0.1 -a jetty,groovy,idea -d groovy/org.codehaus.groovy:groovy-all:2.0.5,compile/org.jboss.spec.javax.servlet:jboss-servlet-api_3.0_spec:1.0.2.Final,compile/org.springframework:spring-core:3.1.2.RELEASE,testCompile/junit:junit:4.11
apply plugin : 'java'
apply plugin : 'jetty'
apply plugin : 'groovy'
apply plugin : 'idea'
group = 'org.mikeneck.gradleb.sample'
version = '0.1'
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
repositories {
mavenCentral ()
}
dependencies {
groovy 'org.codehaus.groovy:groovy-all:2.0.5'
compile 'org.jboss.spec.javax.servlet:jboss-servlet-api_3.0_spec:1.0.2.Final'
compile 'org.springframework:spring-core:3.1.2.RELEASE'
testCompile 'junit:junit:4.11'
}
スクリプトなので、荒削りな部分はありますが、
簡単にビルドスクリプトが書けます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.* | |
@ToString (includeNames = true) | |
class BuildGradle { | |
final StringWriter w = new StringWriter() | |
def plugins = [] | |
void apply () { | |
plugins.each { | |
w << "apply plugin : '${it}'\n" | |
} | |
w << '\n' | |
} | |
String project | |
void structure () { | |
def root = (!project)?'src' : "${project}/src" | |
def ant = new AntBuilder () | |
def dirs = plugins.collect {plg -> | |
PLUGIN.values().collect {type -> | |
type.applicable(plg)? type.dir(root, group) : null | |
} | |
}.flatten().unique() - null | |
dirs.each { | |
ant.echo it | |
ant.mkdir dir : it | |
} | |
} | |
enum PLUGIN { | |
JAVA { | |
List<String> TYPE = [ | |
'java', 'groovy', 'scala', 'antlr', 'application', | |
'jetty', 'war', 'osgi', 'checkstyle', 'codenarc', | |
'findbugs', 'jdepend', 'pmd'] | |
List<String> dir (String grp) { | |
["main/java/${grp}", "test/java/${grp}"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, GROOVY { | |
List<String> TYPE = ['groovy', 'codenarc'] | |
List<String> dir (String grp) { | |
["main/groovy/${grp}", "test/groovy/${grp}"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, SCALA { | |
List<String> TYPE = ['scala'] | |
List<String> dir (String grp) { | |
["main/scala/${grp}", "test/scala/${grp}"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, WEB { | |
List<String> TYPE = ['jetty', 'war'] | |
List<String> dir (String grp) { | |
["main/webapp/WEB-INF"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, RESOURCES { | |
List<String> dir (String grp) { | |
["main/resources", "test/resources"] | |
} | |
@Override | |
boolean applicable (String plugin) {true} | |
}; | |
List<String> dir (String root, String grp) { | |
this.dir(grp.replace('.', '/')).collect {"${root}/${it}"} | |
} | |
boolean applicable (String plugin) {} | |
} | |
String version | |
void wVersion () { | |
w << "version = '${version}'\n" | |
} | |
String group | |
void wGroup () { | |
w << "group = '${group}'\n" | |
} | |
String compatibility | |
void wCompatibility () { | |
w << "sourceCompatibility = '${compatibility}'\n" | |
w << "targetCompatibility = '${compatibility}'\n" | |
w << '\n' | |
} | |
def repos = [] | |
void wRepos () { | |
w << 'repositories {\n' | |
if (repos.contains('mavenCentral')) { | |
w << ' mavenCentral ()\n' | |
} else { | |
w << '\n' | |
} | |
def otherRepos = (repos - 'mavenCentral').collect { | |
" url (${it})\n" | |
} | |
if (otherRepos.size() > 0) { | |
w << ' maven {\n' | |
otherRepos.each { | |
w << " ${it}\n" | |
} | |
w << ' }' | |
} | |
w << '}\n' | |
} | |
def dependencies = [] | |
void wDependencies () { | |
w << 'dependencies {\n' | |
dependencies.each { | |
def item = it.split('/') | |
def scp = item[0] | |
def dep = item[1] | |
w << " ${scp} '${dep}'\n" | |
} | |
w << '}\n' | |
} | |
void compile () { | |
apply() | |
wGroup() | |
wVersion() | |
wCompatibility() | |
wRepos() | |
wDependencies() | |
structure() | |
new File((!project)? '' : "${project}" + '/build.gradle').write(w.toString(),'UTF-8') | |
} | |
} | |
def gradleb (args) { | |
def cli = new CliBuilder (usage : 'gradleb -[hpajgvrd]') | |
cli.with { | |
h longOpt : 'help', 'Show usage information' | |
p longOpt : 'project-name', args : 1, argName : 'project', | |
'project name' | |
a longOpt : 'apply-plugins', args : 1, argName : 'plugin', | |
'Apply plugins to the project (you can specify multi plugins separated by commas. default : java)' | |
j longOpt : 'java-compatibility', args : 1, argName : 'compatibility', | |
'Java compatibility for source and target (default : 1.7)' | |
g longOpt : 'group-name', args : 1, argName : 'group', | |
'Group name for the project' | |
v longOpt : 'archive-version', args : 1, argName : 'version', | |
'a version of the project' | |
r longOpt : 'repositories', args : 1, argName : 'repositoryUrls', | |
'repositories to resolve dependencies. As a default maven central repository is included. You can specify more than one repositories separated by semi-colon(;)' | |
d longOpt : 'dependencies', args : 1, argName : 'dependencies', | |
'dependencies required. You should specify dependencies scope/group:artifact:version. You can specify multi dependencies separated by commas. (ex. groovy/org.codehaus.groovy:groovy-all:2.0.5,testCompile/junit:junit:4.11)' | |
} | |
if (args.size () == 0) { | |
cli.usage() | |
return null | |
} | |
def options = cli.parse(args) | |
if (options.h) { | |
cli.usage () | |
return null | |
} | |
def project = '' | |
if (options.p) { | |
project = options.p | |
} | |
def plugins = ['java'] | |
if (options.a) { | |
plugins += options.a.split(',').collect {it.trim()} | |
} | |
plugins.unique() | |
def compatibility = '1.7' | |
if (options.j) { | |
compatibility = options.j | |
} | |
def pattern = ~/^[a-zA-Z]([a-zA-Z0-9]*)(\.[a-zA-Z][a-zA-Z0-9]*)*(\.[a-zA-Z][a-zA-Z0-9]*)?$/ | |
def group | |
if (options.g) { | |
def grp = options.g | |
if (pattern.matcher(grp).matches()) group = grp | |
else new IllegalArgumentException("The package name [${grp}] is invalid group name.").printStackTrace() | |
} | |
def version = '1.0' | |
if (options.v) { | |
version = options.v | |
} | |
def repos = ['mavenCentral'] | |
if (options.r) { | |
def rps = options.r | |
repos += rps.split(';').collect {it.trim()} | |
} | |
def dependencies = [] | |
if (options.d) { | |
def dps = options.d | |
dependencies += dps.split(',').collect {it.trim()} | |
} | |
def buildGradle = new BuildGradle ( | |
project : project, | |
plugins : plugins, | |
version : version, | |
group : group, | |
compatibility : compatibility, | |
repos : repos, | |
dependencies : dependencies | |
) | |
} | |
def gradle = gradleb(args) | |
if (gradle) gradle.compile() | |