Commit 46157637 authored by Christian Meyer's avatar Christian Meyer
Browse files

initial commit with gradle build plugins and .gitignore

parents
# gradle .gitignore by chrisjenx: https://gist.github.com/chrisjenx/6138781
# built application files
*.apk
*.ap_
# files for the dex VM
*.dex
# Java class files
*.class
# libraries
*.jar
# generated files
bin/
gen/
# Local configuration file (sdk path, etc)
local.properties
# Eclipse project files
.classpath
.project
.settings
# Proguard folder generated by Eclipse
proguard/
# Intellij project files
*.iml
*.ipr
*.iws
.idea/
#Gradle
.gradletasknamecache
.gradle/
build/
bin/
=======
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
#*.launch
# CDT-specific
.cproject
# PDT-specific
.buildpath
# sbteclipse plugin
.target
# TeXlipse plugin
.texlipse
# simulation output
*.csv
*_output_*
# gradle generated launch configuration
Run*WithUI.launch
#parameters folder
parameters/
#generated doc
*.pdf
apply plugin: 'groovy'
// workaround to be able to build the plugin with itself
apply plugin: new GroovyScriptEngine(
[file('src/main/groovy').absolutePath,
file('src/main/resources').absolutePath] as String[],
this.class.classLoader
).loadScriptByName('de/zmt/gradle/ZmtLibraryPlugin.groovy')
description = 'Gradle plugins for build ZMT libraries and simulations'
dependencies {
repositories {
jcenter()
}
compile gradleApi()
compile localGroovy()
}
// dependencies needed in buildscript because plugins are build with itselves
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.hidetake:gradle-ssh-plugin:1.+'
classpath 'nu.studer:gradle-credentials-plugin:1.+'
classpath 'be.jlr-home.gradle:plantumlPlugin:0.1.+'
/* specify here again to override v8001 in
plantuml plugin and get a newer version */
classpath 'net.sourceforge.plantuml:plantuml:8+'
}
}
package de.zmt.gradle;
import org.gradle.api.*
/*
* Base plugin for zmt.core projects
* <p>
* Sets up dochost repository, project group and version.
*/
@Deprecated
class ZmtBasePlugin implements Plugin<Project> {
private static final def CONFIGURE = {
repositories {
jcenter()
// zmt maven repository - intranet only
maven {
url "http://dochost.intra.zmt/maven2/"
}
}
group = 'de.zmt'
version = obtainVersionFromGit(project)
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
/*
* Gets the version name from the latest Git tag
* http://ryanharter.com/blog/2013/07/30/automatic-versioning-with-git-and-gradle/
* http://mrhaki.blogspot.de/2015/04/gradle-goodness-use-git-commit-id-in.html
*/
private static String obtainVersionFromGit(Project project) {
def output = new ByteArrayOutputStream()
try {
project.exec {
commandLine 'git', 'describe'
standardOutput = output
}
}
catch (org.gradle.process.internal.ExecException e) {
println "git describe for ${project.name} failed. Just using commit abbreviated commit id."
output = new ByteArrayOutputStream()
project.exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = output
}
}
return output.toString().trim()
}
}
package de.zmt.gradle;
import org.gradle.api.*
import groovy.swing.*
/*
* Plugin for deployment of zmt.core projects
* <p>
* With this plugin a task 'determineCredentials' is added to the project
* that asks the user to enter login credentials.
*/
class ZmtDeployPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
// https://discuss.gradle.org/t/plugin-not-found-when-i-using-a-gradle-from-other-gradle-file/5628
apply plugin: org.hidetake.gradle.ssh.plugin.SshPlugin
apply plugin: nu.studer.gradle.credentials.CredentialsPlugin
remotes {
dochost {
host = 'dochost'
knownHosts = allowAnyHosts
user = credentials.dochostUser
password = credentials.dochostPw
// webserver directory
}
}
task('determineCredentials') {
description = 'Ask user to enter credentials.'
ext.wwwPath = '/var/www/dochost/'
onlyIf { !remotes.dochost.user || !remotes.dochost.password }
}
determineCredentials.doLast {
def console = System.console()
// we can access console: ask for username / password
if (console) {
console.printf("\nEnter username: ")
remotes.dochost.user = console.readLine()
console.printf("\nEnter password: ")
// readPassword() returns char array, wrap it into string
remotes.dochost.password = new String(console.readPassword())
}
// no console: create dialog and get it from there
else {
// https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files/
new SwingBuilder().edt {
dialog(
modal: true, // Otherwise the build will continue running before you closed the dialog
title: 'Connecting...', // Dialog title
alwaysOnTop: true, // pretty much what the name says
resizable: false, // Don't allow the user to resize the dialog
locationRelativeTo: null, // Place dialog in center of the screen
pack: true, // We need to pack the dialog (so it will take the size of it's children)
show: true // Let's show it
) {
vbox {
label(text: "username: ")
userInput = textField(text: System.getProperty("user.name"))
userInput.selectAll()
label(text: "password: ")
pwInput = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
remotes.dochost.user = userInput.text
remotes.dochost.password = pwInput.text
dispose() // Close dialog
})
}
}
}
}
println "Use task 'addCredentials' with keys 'dochostUser' and 'dochostPw' if you want to store credentials permanently."
}
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
}
package de.zmt.gradle;
import org.gradle.api.*
/*
* Distribution plugin for simulations based on zmt.core
* <p>
* With this plugin the simulation's jar file, source, dependencies
* and the runtime dependencies are added to the distribution's lib
* folder. A CHANGES file is also added containing git tags and
* their descriptions.
*/
public class ZmtDistributionPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply plugin: 'distribution'
distributions {
main {
contents {
with (rootProject.distributions.main.contents)
into ('lib') {
// project's own library and dependencies
from (project.jar)
from (project.sourceJar)
from (project.configurations.runtime)
}
// prevent adding duplicates
// e.g. when several projects depend on the same library
// otherwise they are added several times to archives
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}
}
task deployZip {
group = 'Upload'
description = 'Uploads zip distribution to dochost.'
dependsOn distZip, determineCredentials
}
deployZip.doLast {
println "\nConnecting to ${remotes.dochost.host}..."
ssh.run {
session(remotes.dochost) {
def remoteTargetDir = determineCredentials.wwwPath + 'dist'
// create target directory on remote
execute "mkdir -p ${remoteTargetDir}"
put from: distZip.archivePath, into: remoteTargetDir
// create CHANGES file from git version list
put text: obtainVersionListFromGit(it), into: remoteTargetDir + '/CHANGES'
}
}
}
}
private static String obtainVersionListFromGit(Project project) {
def output = new ByteArrayOutputStream()
try {
exec {
commandLine 'git', 'tag', '-n99'
standardOutput = output
}
}
catch (org.gradle.process.internal.ExecException e) {
println "git tag for ${project.name} failed. Cannot obtain version list."
output = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = output
}
}
return output.toString().trim()
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
}
package de.zmt.gradle;
import org.gradle.api.*
/*
* Eclipse Plugin for simulations based on zmt.core
* <p>
* With this plugin a launch configuration will be generated to launch the
* simulation in eclipse via launcher.
*/
public class ZmtEclipsePlugin implements Plugin<Project> {
private static final def CONFIGURE = {
// need to have dependencies and stuff evaluated
afterEvaluate {
// also generate eclipse files in dependencies
configurations.compile.dependencies.withType(ProjectDependency).each {
// need to evaluate dependencies first
evaluationDependsOn(it.dependencyProject.path)
if (it.dependencyProject.plugins.hasPlugin('eclipse')) {
tasks.eclipse.dependsOn(it.dependencyProject.tasks.eclipse)
tasks.cleanEclipse.dependsOn(
it.dependencyProject.tasks.cleanEclipse)
}
}
// ... and for launcher
tasks.eclipse.dependsOn(':zmt.launcher:eclipse')
tasks.cleanEclipse.dependsOn(':zmt.launcher:cleanEclipse')
/* add runtime dependency for this project in launcher
to make it executable in eclipse */
project(':zmt.launcher').afterEvaluate {
project(':zmt.launcher').dependencies {
runtime(project) {
transitive = false
}
}
}
}
def upperCaseName = name.substring(0, 1).toUpperCase()
upperCaseName += name.substring(1)
def launchConfigFileName = "Run${upperCaseName}WithUI.launch"
tasks.eclipse.doLast {
def launchConfig = new File(launchConfigFileName)
// eclipse generated launch configuration with project name in it
launchConfig.text = """<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
<launchConfiguration type=\"org.eclipse.jdt.launching.localJavaApplication\">
<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_PATHS\">
<listEntry value=\"/zmt.launcher/src/main/java/de/zmt/Main.java\"/>
</listAttribute>
<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_TYPES\">
<listEntry value=\"1\"/>
</listAttribute>
<stringAttribute key=\"org.eclipse.jdt.launching.MAIN_TYPE\" value=\"de.zmt.Main\"/>
<stringAttribute key=\"org.eclipse.jdt.launching.PROGRAM_ARGUMENTS\" value=\"$project.name gui\"/>
<stringAttribute key=\"org.eclipse.jdt.launching.PROJECT_ATTR\" value=\"zmt.launcher\"/>
<stringAttribute key=\"org.eclipse.jdt.launching.WORKING_DIRECTORY\" value=\"\${workspace_loc:$project.name}\"/>
</launchConfiguration>"""
}
// delete launch configuration
tasks.cleanEclipse.doLast {
new File(launchConfigFileName).delete()
}
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
}
package de.zmt.gradle;
import org.gradle.api.*
import org.gradle.api.tasks.bundling.Jar
/**
* Custom plugin for java projects in zmt.core
* <p>
* With this script a java project is set up with several custom tasks.
*/
class ZmtJavaPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
testCompile 'org.mockito:mockito-core:2.+'
}
task('testsJar', type: Jar) {
group = 'Build'
description = 'Assembles a jar archive containing the test classes'
classifier 'tests'
from sourceSets.test.output
}
task('listJar') {
group = 'Help'
description = 'Lists the libraries this project depends on.'
doLast {
configurations.compile.each { File file -> println file.name }
}
}
task('sourceJar', type: Jar) {
group = 'Build'
description = 'Assembles a jar archive containing sources.'
classifier = 'sources'
from sourceSets.main.allSource
}
task('javadocJar', type: Jar, dependsOn: 'javadoc') {
group = 'Build'
description = 'Assembles a jar archive containing javadoc.'
classifier = 'javadoc'
from javadoc.destinationDir
}
// for creating a tests jar file
configurations {
tests
}
artifacts {
tests testsJar
}
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
}
package de.zmt.gradle;
import org.gradle.api.*
import org.gradle.external.javadoc.JavadocOfflineLink
import org.gradle.plugins.ide.eclipse.model.ProjectDependency
/*
* Plugin for adding links to dependency projects to javadoc task
* <p>
* Also adds a task to deploy javadoc files.
*/
class ZmtJavadocPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply plugin: be.jlrhome.gradle.plantuml.PlantumlPlugin
// use svg instead of the default (png)
plantuml.fileFormat = net.sourceforge.plantuml.FileFormat.SVG
// do javadoc setup after evaluating project build files
afterEvaluate {
def projectDependencies = configurations.compile.dependencies.withType(ProjectDependency)
// making compile dependency project evaluate before
projectDependencies.each {
dep -> evaluationDependsOn(dep.dependencyProject.path)
}
// add javadoc links from compile dependencies
// that have been evaluated before
project.javadoc {
ext.targetPath = 'javadoc/' + project.name
// intranet target javadoc url for this project
ext.targetUrl = 'http://dochost.intra.zmt/' + targetPath
options {
// add java api link to all projects
links << "http://docs.oracle.com/javase/7/docs/api/"
// get links from dependencies
projectDependencies.each {
def dependencyJavadoc = it.dependencyProject.javadoc
// add links of dependency
links.addAll(
dependencyJavadoc.options.links
)
// and offline link to dependency's target url
linksOffline.add(new JavadocOfflineLink(
dependencyJavadoc.targetUrl,
dependencyJavadoc.destinationDir.path
))
links.add(dependencyJavadoc.targetUrl)
// remove duplicates
links = links.unique()
}
}
}
}
task('deployJavadoc') {
dependsOn 'javadoc', 'determineCredentials'
group = 'Upload'
description = 'Uploads javadoc files to dochost.'
}
deployJavadoc.doLast {
println "\nConnecting to ${remotes.dochost.host}..."
ssh.run {
session(remotes.dochost) {
def remoteTargetDir = determineCredentials.wwwPath + javadoc.targetPath
// create and clean target directory on remote
execute "mkdir -p ${remoteTargetDir}"
execute "rm -rf ${remoteTargetDir}/*"
javadoc.destinationDir.eachFile {
put from: it, into: remoteTargetDir
}
}
}
}
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
}
package de.zmt.gradle;
import org.gradle.api.*
/*
* Meta plugin applying all necessary plugins for ZMT libraries
*/
public class ZmtLibraryPlugin implements Plugin<Project> {
private static final def CONFIGURE = {
apply(plugin: ZmtBasePlugin)
apply(plugin: ZmtJavaPlugin)
apply(plugin: ZmtJavadocPlugin)
apply(plugin: ZmtDeployPlugin)
apply(plugin: ZmtPublishPlugin)
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
}