Select Git revision
tycho_build.gradle
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
tycho_build.gradle 17.21 KiB
/*
Build Script can be executed via 'gradle install'
Build Script Dependencies can be downloaded via 'gradle collectDepenencies'
Executing the Build Script and download Dependencies can be executed via 'gradle completeInstall'
For a complete classPath Refresh please execute 'gradle deleteFromClassPath setClassPath'
Pom Generation can be executed via gradle deploy
If you have a product definition please add a buildProduct = true to your main build.gradle script
*/
apply plugin: 'base'
import groovy.io.FileType
project.ext.tychoVersion = "4.0.8"
try {
workspacePath = workspacePath
} catch (MissingPropertyException e) {
project.ext.workspacePath = ""
}
try {
dependencyFolder = dependencyFolder
} catch (MissingPropertyException e) {
project.ext.dependencyFolder = "lib/dependencies/" // Folder in each subproject where
}
try {
features = features
} catch (MissingPropertyException e) {
project.ext.features = []
for (int i = 0; i < subprojects.name.size(); i++){
if (subprojects.name[i] ==~ /.*\.[fF]eature/) {
features.add(subprojects.name[i])
}
}
}
try {
repositoryName = repositoryName
} catch (MissingPropertyException e) {
project.ext.repositoryName = groupID + ".repository"
}
try {
parentID = parentID
} catch (MissingPropertyException e) {
project.ext.parentID = groupID + ".parent"
}
// Add certain Jar to Library
def addLibToCP(def project, def libPar) {
def filePar = new File(workspacePath + project + "/.classpath")
boolean notYetAdded = true
filePar.eachLine {line ->
def pattern = ".*${dependencyFolder}${libPar}.*"
if (line ==~ /${pattern}/) {
notYetAdded = false
}
}
if (notYetAdded) {
File newCP = new File(workspacePath + project + '/.cp')
filePar.eachLine {line ->
if (line ==~ /.*<\/classpath>.*/) {
String entry = '\t<classpathentry exported="true" kind="lib" path="' + dependencyFolder + libPar + '"/>' + '\n</classpath>\n'
line = line.replaceAll(/<\/classpath>/, entry)
println project + " : " + libPar + " added to classpath"
newCP << line
} else {
newCP << line + "\n"
}
}
filePar.delete()
newCP.renameTo(workspacePath + project + '/.classpath')
} else {
println project + " : " + libPar + " classpath entry already present"
}
}
// deletes whole DependenciesLibrary Folder
def deleteLibFromCP(def project) {
def filePar = new File(workspacePath + project + "/.classpath")
boolean deleteCP = false
def newCP = new File(workspacePath + project + '/cp')
def pattern = ".*${dependencyFolder}[^<].*/>"
filePar.eachLine {line ->
if (line ==~ /\n(\ |\t)*/) {
// delete empty lines
line = line.replaceAll(/\n(\ |\t)*/, '')
}
if (line ==~ /${pattern}/) {
deleteCP = true
def pattern2 = '(\\ |\\t)*<classpathentry exported="true" kind="lib" path="' + dependencyFolder + "[^<].*"+'"/>(\\ |\\t)*'
line = line.replaceAll(/${pattern2}/, '')
newCP << line
} else {
newCP << line+"\n"
}
}
if (!deleteCP) {
println project + " : no dependencies from " + dependencyFolder + " found in classpath file! "
newCP.delete()
} else {
filePar.delete()
newCP.renameTo(workspacePath + project + '/.classpath')
println workspacePath + project + '/.classpath' + "!"
}
}
// Adds a Library to the current Bundle-ClassPath of the Manifest file
def addRunTimeLib(String libPar, String projectPar) {
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
def newMf = new File(projectPar + "/MF")
newMf.delete()
mf.eachLine {line ->
if (line ==~ /.*Bundle-ClassPath:.*\.jar.*/) {
def libsInLine = line.replaceAll(/[\ \t]*Bundle-ClassPath:[\ \t]*/, '')
line = line.replaceFirst(/[^\ \t]*\.jar[\ \,]*/, dependencyFolder + libPar + ",\n " + libsInLine) // /[^\ \t]*.\.jar[\ \,]*/
} else {
if (line ==~ /.*Bundle-ClassPath: \.[\ \t]*/) {
line = line + ",\n " + dependencyFolder + libPar
} else if (line ==~ /.*Bundle-ClassPath: \.\,[\ \t]*/) {
line = line + "\n " + dependencyFolder + libPar + ","
}
}
newMf << line + "\n"
}
mf.delete()
newMf.renameTo(projectPar + "/META-INF/MANIFEST.MF")
}
// Creates a Bundle-ClassPath Section in the Manifest file
def createRunTimeLib(String libPar, String projectPar) {
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
mf << "Bundle-ClassPath: lib/dependencies/" + libPar
}
// checks for a Bundle-ClassPath section in the Manifest file
def boolean checkForRunTimeLibs(String projectPar) {
boolean returnBool = false
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
mf.eachLine {line ->
if (line ==~ /.*Bundle-ClassPath:.*/) {
returnBool = true
}
}
return returnBool
}
// checks if lib is already present in Manifest file
// if lib is present but is referenced from another folder than dependencyFolder the library from dependencyFolder will be referenced
def boolean checkRunTimeLib(String libPar, String projectPar){
// checks if lib is in RunTimeLibrary present and changes reference if necessary
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
def newMf = new File(projectPar + "/MF")
newMf.delete()
boolean returnBool = false
boolean changed = false
mf.eachLine {line ->
if (line ==~ /.*${libPar}.*/) {
if (!(line ==~ /.*(\ |\t)*${dependencyFolder}${libPar}(\ |\t)*.*/)){
// if lib is already referenced
// reference lib from dependencyFolder
line = line.replaceFirst( /[^\ \t]*${libPar}/, dependencyFolder+libPar )
changed = true
println line
println "yolo ${libPar}\n"
}
returnBool = true
}
newMf << line + "\n"
}
if (changed) {
mf.delete()
newMf.renameTo(projectPar + "/META-INF/MANIFEST.MF")
} else {
newMf.delete()
}
return returnBool
}
///////////////////////////////////
// !!! DEFINING SUB PROJECTS !!! //
///////////////////////////////////
subprojects {
apply plugin: 'base'
apply plugin: 'java'
// apply plugin: 'eclipse'
task deleteArtifacts(type: Delete) {
delete 'target', 'pom.xml', dependencyFolder
}
// Copy Dependencies into subprojects DependencyFolder
task collectDependencies(type: Copy) {
from configurations.runtimeClasspath
into "${dependencyFolder}"
}
task setClassPath(dependsOn: 'collectDependencies') {
description = "\tAdds all your Dependencies from your local lib folder in each project to it's classpath"
doLast {
def dependencyList = []
def depsFolder = new File(workspacePath + project.name + "/" + dependencyFolder)
if (depsFolder.exists()) {
depsFolder.eachFile() {file ->
if (!(file.getName() ==~ /.*\.txt/)) {
dependencyList << file.getName()
}
}
if (features.every {it != project.name}) {
boolean BundleClassPathPresent = checkForRunTimeLibs(project.name)
for (int icp = 0; icp < dependencyList.size; icp++) {
// Adds Lib to .classPath file
addLibToCP(project.name, dependencyList[icp])
if (!BundleClassPathPresent) {
// Adds Lib to Manifest File
createRunTimeLib(dependencyList[icp], project.name)
BundleClassPathPresent = true
} else if (!checkRunTimeLib(dependencyList[icp], project.name)) {
// if library not present add it to RunTimeLibrary
addRunTimeLib(dependencyList[icp], project.name)
}
}
// could still be usefull for debugging, that's why it's not deleted
/*dependencyList.each {dep ->
println project.name + ": " + dep
}*/
}
def warningReadMe = new File(workspacePath + project.name + "/" + dependencyFolder + "_README.txt")
warningReadMe.delete()
warningReadMe << "Do Not Remove any Jars/Libraries in this Folder!\nThis folder contains all of your dependencies defined in your gradle script.\n"
warningReadMe << "Removing or renaming any of these files will result in an Error in your .classpath file\n"
warningReadMe << "If any error concerning missing dependencies should occur please run 'gradle deleteFromClassPath setClassPath' in your workspace folder from your shell."
}
}
}
task deleteFromClassPath(){
description = "\tDeletes all your Dependencies located in your local lib folder from each project's classpath"
doLast {
boolean depsDelete = false
def depsFolder = new File(workspacePath + project.name + '/' + dependencyFolder)
if (depsFolder.exists()) {
depsDelete = true
}
depsFolder.deleteDir()
if (features.every {it != project.name} && depsDelete) {
deleteLibFromCP(project.name)
}
}
}
// For a complete classPath Refresh please execute 'gradle deleteFromClassPath setClassPath'
////////////////////////////////////////////////////////////////////////////////////////////
task deploy() {
description = "\tGenerating the Tycho Poms. Please remember to add a '.qualifier' to the version numbers!"
doLast {
String versionNumber = 'Version Number Error:\tcheck Manifest for Bundle-Version Number and make sure to add a ".qualifier" to the version numbers!\n'
String artifactId = 'Could not find Bundle-SymbolicName in Manifest.file'
// -- In case of changed Manifest File in Eclipse:
// Version Numbers of the projects are collected via
// regular expressions in the Manifest.MF File.
// versionnumber of the projects are equal to their
// Bundle-Version Number
// Generating Poms for sub projects except features
if (features.every {it != project.name}) {
def content = new File(workspacePath + "${project.name}/META-INF/MANIFEST.MF").getText("UTF-8")
def printFileLine = {
if (it ==~ /Bundle-Version.+qualifier/) {
versionNumber = it.substring(16) // possibile error: cuts off first 16 chars
// Version Number is taken from Bundle-Version in Manifest.MF
// If there is no Bundle-Version or the versionnumber needs to
// be taken from a different key word, please change the
// regular expression and the substring above
versionNumber = versionNumber.replace(".qualifier", "-SNAPSHOT")
}
// Artifact ID is taken from Bundle-SymbolicName minus the 16 chars ';singleton:=true'
if (it ==~ /Bundle-SymbolicName:.+/) {
artifactId = it
artifactId = artifactId.replace("Bundle-SymbolicName:", '')
artifactId = artifactId.replace(";", '')
artifactId = artifactId.replace(" ", '')
artifactId = artifactId.replace("singleton:=true", '')
}
}
content.eachLine(printFileLine)
def f = new File(workspacePath + artifactId + '/pom.xml')
f.delete()
if (project.name ==~ /.*\.[tT]ests*/) {
// Test Cases
f << start() + elder() + testArtifact(artifactId, versionNumber) + end()
} else {
// Normal Plugin
f << start() + elder() + artifact(artifactId, versionNumber) + end()
// old pom.xml files are deleted and replaced by new auto generated Tycho pom.xml files
}
} else if (features.any {it == project.name}) {
// Features
def parsedXml = new XmlParser().parse(workspacePath + "${project.name}/feature.xml")
artifactId = parsedXml.attribute("id")
versionNumber = parsedXml.attribute("version")
versionNumber = versionNumber.replace(".qualifier", "-SNAPSHOT")
def f = new File(workspacePath + artifactId + '/pom.xml')
f.delete()
f << feature(artifactId, versionNumber)
}
}
}
}
clean {
dependsOn += subprojects.deleteArtifacts
}
// define Parent
task createParent() {
doLast {
new File(workspacePath + "${parentID}").mkdir()
String versionNumber = '1.0.0.qualifier'
def f = new File(workspacePath + parentID + '/pom.xml')
f.delete()
f << parentPom(parentID)
for (int i = 0; i < targetRepositories.size(); i++) {
f << repos(targetRepositories[i], i)
}
f << endRepos()
f << moduleStart()
subprojects.each {subproj ->
f << module(subproj.name)
}
f << module(repositoryName)
f << endParent()
}
}
task deleteParent(type: Delete) {
delete(workspacePath + "${parentID}")
}
clean.dependsOn(deleteParent)
// define Repository
task createRepository() {
doLast{
def pom = new File(workspacePath + repositoryName + '/pom.xml')
pom.delete()
pom << reposi()
}
}
task deleteRepository(type: Delete) {
delete(workspacePath + repositoryName + "/pom.xml")
delete(workspacePath + repositoryName + "/target")
}
clean.dependsOn(deleteRepository)
task createPoms(dependsOn: [createParent, createRepository, subprojects.deploy])
task install(dependsOn: [createPoms], type: Exec) {
description = "\tExecutes a 'mvn install' of the parent pom.xml and auto-generates Tycho Poms"
commandLine 'mvn', 'install', '-f', workspacePath + parentID + '/pom.xml'
}
task prepareMaven(dependsOn: [subprojects.collectDependencies, createPoms])
task completeInstall(dependsOn: [prepareMaven], type: Exec) {
description = "\tCopies dependencies into dependencyFolder of each subproject and executes a 'mvn install' of the parent pom.xml and auto-generates Tycho Poms"
commandLine 'mvn', 'install', '-f', workspacePath + parentID + '/pom.xml'
}
// --- Defining Tycho POM parts -- //
def artifact(artifactId,versionNumber) {"""
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${versionNumber}</version>
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>\${tycho-version}</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
"""}
def testArtifact(artifactId,versionNumber) {"""
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${versionNumber}</version>
<packaging>eclipse-test-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>\${tycho-version}</version>
</plugin>
</plugins>
</build>
"""}
def start() {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
"""}
def elder() {"""
<parent>
<groupId>${groupID}</groupId>
<artifactId>${parentID}</artifactId>
<version>1.0.0.qualifier</version>
<relativePath>../${parentID}/pom.xml</relativePath>
</parent>
"""}
def end() {"""
</project>
"""}
// -- defining Parent Pom -- //
def parentPom(artifactId) {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>1.0.0.qualifier</version>
<packaging>pom</packaging>
<!-- this is the parent POM from which all modules inherit common settings -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tycho-version>${tychoVersion}</tycho-version>
</properties>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<repositories>
<!-- configure p2 repository to resolve against -->
"""}
def repos(String targetRepo, int i) {"""
<repository>
<id>targetRepository${i}</id>
<layout>p2</layout>
<url>${targetRepo}</url>
</repository>
"""}
def endRepos() {"""
</repositories>
<build>
<plugins>
<plugin>
<!-- enable tycho build extension -->
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tychoVersion}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
"""}
def moduleStart() {"""
<!-- the modules that should be built together -->
<modules>
"""}
def module(String project) {"""\
<module>../${project}</module>
"""}
def endParent() {"""\
</modules>
</project>
"""}
// end of defining parent pom.xml
// repository Pom
def reposi() {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${groupID}</groupId>
<artifactId>${parentID}</artifactId>
<version>1.0.0.qualifier</version>
<relativePath>../${parentID}/pom.xml</relativePath>
</parent>
<groupId>${groupID}</groupId>
<artifactId>${repositoryName}</artifactId>
<version>1.0.0.qualifier</version>
<packaging>eclipse-repository</packaging>
</project>
"""}
// feature pom
def feature(artifactId, versionNumber) {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${groupID}</groupId>
<artifactId>${parentID}</artifactId>
<version>1.0.0.qualifier</version>
<relativePath>../${parentID}/pom.xml</relativePath>
</parent>
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${versionNumber}</version>
<packaging>eclipse-feature</packaging>
</project>
"""}
// end of feature pom
// Build Script can be executed via gradle install
// For a complete classPath Refresh please execute 'gradle deleteFromClassPath setClassPath'
// Pom Generation can be executed via gradle deploy