Gradle how to include files conditionally? - if-statement

Gradle: how to include different files conditionally?
For example,
file1.js
file2.js
file3.js
file4.js
from (zipTree("/foo/bar.jar")) {
include {
if (project.hasProperty("debug"))
//file1.js, file2.js
else
//file3.js, file4.js
}
}

For example like
from(zipTree('/foo/bar.jar')) {
if (project.hasProperty('debug'))
include 'file1.js', 'file2.js'
else
include 'file3.js', 'file4.js'
}
or like
from(zipTree('/foo/bar.jar')) {
include project.hasProperty('debug') ? ['file1.js', 'file2.js'] : ['file3.js', 'file4.js']
}

Related

JobDSL custom config file

I'm trying to setup a jenkins job with custom config file, original xml looks as follows (the relevant part):
<buildWrappers>
<org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper plugin="config-file-provider#2.11">
<managedFiles>
<org.jenkinsci.plugins.configfiles.buildwrapper.ManagedFile>
<fileId>30de8d2f-621d-4c51-b644-4302b548fd15</fileId>
<targetLocation>./src/</targetLocation>
<variable/>
<replaceTokens>false</replaceTokens>
</org.jenkinsci.plugins.configfiles.buildwrapper.ManagedFile>
</managedFiles>
</org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper>
</buildWrappers>
Here's my JobDSL attempt:
job('example') {
configure{
it / 'buildWrappers' << 'org.jenkinsci.plugins.configfiles.buildwrapper.ManagedFile' {
managedFiles {
org.jenkinsci.plugins.configfiles.buildwrapper.ManagedFile{
fileId '30de8d2f-621d-4c51-b644-4302b548fd15'
targetLocation './/src//'
}
}
}
}
}
What am I missing? Thanks!
You can use the built-in DSL: https://jenkinsci.github.io/job-dsl-plugin/#path/job-wrappers-configFiles
The built-in DSL will also resolve the fileId from the file name.
job('example') {
wrappers {
configFiles {
file('myCustomConfigFile') {
targetLocation('src')
}
}
}
}

How to list files and folders together in liferay?

I am trying to list all the files and folders under the root folder of liferay site.
QueryDefinition queryDefinition = new QueryDefinition(WorkflowConstants.STATUS_ANY, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
List<Object> list = DLFolderLocalServiceUtil.getFoldersAndFileEntriesAndFileShortcuts(groupId, folderId, null, true, queryDefinition);
Is it the right way? How to differentiate the files and folders?
You can get and differentiate all files, folders and shortcuts as following:
List <Object> foldersAndFileEntriesAndFileShortcuts =
DLAppServiceUtil.getFoldersAndFileEntriesAndFileShortcuts(
folder.getGroupId(), folderId, WorkflowConstants.STATUS_ANY,
true, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
for (Object folderAndFileEntryAndFileShortcut: foldersAndFileEntriesAndFileShortcuts) {
if (folderAndFileEntryAndFileShortcut instanceof FileEntry) {
FileEntry fileEntry = (FileEntry) folderAndFileEntryAndFileShortcut;
} else if (folderAndFileEntryAndFileShortcut instanceof Folder) {
Folder subFolder = (Folder) folderAndFileEntryAndFileShortcut;
} else if (folderAndFileEntryAndFileShortcut instanceof DLFileShortcut) {
DLFileShortcut dlFileShorcut = (DLFileShortcut) folderAndFileEntryAndFileShortcut;
}
}
There is a util called: DLAppServiceUtil with method: getFoldersAndFileEntriesAndFileShortcuts. Check out this this link (liferay code).

Adding native static dependencies

In a C++ Gradle project, I use the boost 1.49 library.
How can I add this library as dependency of my project.
I try in this way, but I should define boost as dependency. There is a C++ library repository?
apply plugin: 'cpp'
libraries {
hello {
baseName 'hello'
}
}
sources {
hello{
cpp {
source.srcDirs = ['src']
lib library: 'boost', linkage: 'api'
exportedHeaders.srcDirs = ['include']
}
}
}
c/cpp programming is not but domain (in contrast to gradle ;) ) but when you navigate to $GRADLE_HOME/samples/native-binaries you can find lots of useful examples there.
One of them - cunit (oh, there's a another one: multi-project) has such configuration - it may be useful for you. Below I enclose build.gradle from cunit - in case you use gradle wrapper.
apply plugin: "c"
apply plugin: "cunit"
model {
flavors {
passing
failing
}
repositories {
libs(PrebuiltLibraries) {
cunit {
headers.srcDir "lib/cunit/2.1-2/include"
binaries.withType(StaticLibraryBinary) {
staticLibraryFile =
file("lib/cunit/2.1-2/lib/" +
findCUnitLibForPlatform(targetPlatform))
}
}
}
}
}
libraries {
operators {}
}
binaries.withType(CUnitTestSuiteBinarySpec) {
lib library: "cunit", linkage: "static"
if (flavor == flavors.failing) {
cCompiler.define "PLUS_BROKEN"
}
}
def findCUnitLibForPlatform(Platform platform) {
if (platform.operatingSystem.windows) {
return "vs2010/cunit.lib"
// return "vs2013/cunit.lib"
// return "cygwin/cunit.lib"
// return "mingw/cunit.lib"
} else if (platform.operatingSystem.macOsX) {
return "osx/libcunit.a"
} else {
return "linux/libcunit.a"
}
}
Quick googling has shown that there's no such thing like a libraries repository for c/cpp projects. It seems that the libraries should be included in project sources.

Android studio, assembleDebugTest cause Multiple dex files define

trying to run android tests in android studio 0.8.1
I can run assembleDebug and assembleTest properly. But when I try to run the android test it calles assembleDebug and assembleDebugTest and with the latter I get the problem with 'Multiple dex files define'
a few pictures:
and the build.gradle of the project
dependencies {
compile project(':libraries:someLib')
compile ('com.google.android.gms:play-services:5.+')
compile ('fr.avianey:facebook-android-api:+#aar')
compile ('com.fasterxml.jackson.core:jackson-databind:2.3.1')
compile ('com.fasterxml.jackson.core:jackson-core:2.3.1')
compile ('com.fasterxml.jackson.core:jackson-annotations:2.3.0')
compile fileTree(dir: 'libs', include: '*.jar')
//androidTestCompile 'junit:junit:4.10'
//androidTestCompile 'org.robolectric:robolectric:2.1.+'
//androidTestCompile 'com.squareup:fest-android:1.0.+'
//androidTestCompile 'org.powermock:powermock-api-mockito:1.5.1'
}
and the build.gradle of the "someLib"
dependencies {
compile ('com.android.support:appcompat-v7:19.1.+')
compile ('com.nineoldandroids:library:2.4.0')
compile (group: 'com.google.guava', name: 'guava', version: '16.0-rc1')
}
they both share the rest
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'com.android.library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
}
lintOptions {
abortOnError false
}
}
So far I tried 2 solutions that seems to work but I am pretty sure there has to be something better.
Anyway 1 solution:
tasks.whenTaskAdded { theTask ->
if("assembleDebugTest".toString().equals(theTask.name.toString())) {
def yourTaskName = "cleanLibs"
project.task(yourTaskName) << {
println "${project.buildDir}/intermediates/pre-dexed/test/debug/"
delete fileTree(dir: ("${project.buildDir}/intermediates/pre-dexed/test/debug/"))
}
theTask.dependsOn(yourTaskName)
def processTask = "preDexDebugTest"
project.(yourTaskName.toString()).dependsOn(processTask)
project.(processTask.toString()).dependsOn("compileDebugTestJava")
}
}
this removes the libs before they are created again, it is slow and ungly, let the libs be created, removes them and creates them again
solution 2:
tasks.whenTaskAdded { theTask ->
if("assembleDebugTest".toString().equals(theTask.name.toString())) {
def processTask = "preDexDebugTest"
project.(processTask.toString()).enabled = false
}
}
just skip the task that create the first libs, this is way better than the first one
then for both you have to extend
android {
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'com/flurry/org/codehaus/jackson/map/VERSION.txt'
exclude 'com/flurry/org/codehaus/jackson/impl/VERSION.txt'
exclude 'com/flurry/org/apache/avro/data/Json.avsc'
exclude 'META-INF/ASL2.0'
}
}
by whatever is twice

get the list of source files from existing eclipse project using CDT

i am working on CDT eclipse plug in development, i am trying to get the list of sources files which exist in eclipse project explorer using CDT code using following code ,which results null.
Case1:
IFile[] files2 = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(new URI("file:/"+workingDirectory));
for (IFile file : files2) {
System.out.println("fullpath " +file.getFullPath());
}
Case2:
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(getProject().getRawLocationURI());
for (IFile file : files) {
System.out.println("fullpath " +file.getFullPath());
}
Case3:
IFile[] files3 = ResourceLookup.findFilesByName(getProject().getFullPath(),ResourcesPlugin.getWorkspace().getRoot().getProjects(),false);
for (IFile file : files3) {
System.out.println("fullpath " +file.getFullPath());
}
Case4:
IFolder srcFolder = project.getFolder("src");
Case 1 ,2,3 giving me output null, where i am expecting list of files;
in Case 4: i am getting the list of "helloworld/src" files, but i am expecting to get the files from the existing project mean main root ,ex:"helloworld"
please suggest me on this.
You can either walk through the worspace resources tree using IResourceVisitor - or you can walk through CDT model:
private void findSourceFiles(final IProject project) {
final ICProject cproject = CoreModel.getDefault().create(project);
if (cproject != null) {
try {
cproject.accept(new ICElementVisitor() {
#Override
public boolean visit(final ICElement element) throws CoreException {
if (element.getElementType() == ICElement.C_UNIT) {
ITranslationUnit unit = (ITranslationUnit) element;
if (unit.isSourceUnit()) {
System.out.printf("%s, %s, %s\n", element.getElementName(), element.getClass(), element
.getUnderlyingResource().getFullPath());
}
return false;
} else {
return true;
}
}
});
} catch (final CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Note there may be more source files then you actually want (e.g. you may not care system about headers) - you can filter them by checking what the underlying resource is.