Gradle build not including source/src groovy - groovy

I am trying to create a jar from a basic program.
I have a basic groovy project i.e. src/org...../*.groovy In the root
I have the following build.gradle
apply plugin: 'groovy'
version = '1.0'
repositories {
mavenCentral();
}
dependencies
{
compile files (fileTree(dir: 'lib', include: ['*.jar']),
fileTree(dir: 'lib/DocxDep', include: ['*.jar']))
}
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output.classesDir)
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Implementation-Title': 'Lab Services',
'Implementation-Version': version,
'Main-Class': 'org.xxx.clarity.ClarityServices'
}
}
Problem is when I run and/or inspec the jar file my sclasses from src/** are not included! (all the dependencies are perfect)
What is the problem here?
UPDATE
When I add:
from files(fileTree(dir: 'src'))
to the task it includes the .groovy files :(
When I add
from sourceSets.main.output.classesDir
to the task and:
sourceSets {
main {
groovy {
srcDir 'src'
}
}
}
They do not get included :( Can't find any other ways....

By default, Gradle looks for source in src/main/groovy when the 'groovy' plugin in applied. You'll need to either restructure your project or configure your source sets to appropriately reflect your project structure.

Final working build.gradle. (thanks all).
apply plugin: 'application'
apply plugin: 'groovy'
version = '1.0'
repositories {
mavenCentral();
}
dependencies
{
compile files (fileTree(dir: 'lib', include: ['*.jar']),
fileTree(dir: 'lib/DocxDep', include: ['*.jar']))
compile 'org.codehaus.groovy:groovy-all:2.3.6' //Was missing
}
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output) //Was missing/wrong
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jar
sourceSets.main.groovy {
srcDirs = [ 'src' ] //Was missing/wrong
}
manifest {
attributes 'Implementation-Title': 'Lab Services',
'Implementation-Version': version,
'Main-Class': 'org.petermac.clarity.ClarityServices'
}
}

referencing sourceSets.main.output.classesDir in your jar task means that it will just copy everything from that directory in your jar. The problem is that when you run gradle buildLabServicesJar nothing tells gradle that the classes should be compiled first. That's why the directory keeps to be empty and your jar doesn't contain the compiled classes. If you modify your task declaration from
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output.classesDir)
...
}
to
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output)
...
}
task autowiring kicks in. task autowiring means that if you declare an output of one task as input to another task (your buildLabServicesJar) gradle knows that it must generate the output first (run the compile task for example).
hope that helps!

You must excuse me but I have recently crossed over from a long life of Microsoft and am still learning. I am surprised by the lack of blogs and example code of basic stuff, what I am doing is so standard....(I will be posting one once/if I figure this out)
Note: Intellij -> Build -> Build Artifacts works perfectly but I would like to move this to Bamboo.
anyway taking into account everyone's ideas, here is my file (and error)
apply plugin: 'groovy'
version = '1.0'
repositories {
mavenCentral();
}
dependencies
{
compile files (fileTree(dir: 'lib', include: ['*.jar']),
fileTree(dir: 'lib/DocxDep', include: ['*.jar']))
}
//println "Classes dir: " + sourceSets.main.groovy
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output)
//from sourceSets.main.groovy.output
//from files(fileTree(dir: 'src'))
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Implementation-Title': 'Lab Services',
'Implementation-Version': version,
'Main-Class': 'org.petermac.clarity.ClarityServices'
}
}
sourceSets {
main {
groovy.srcDirs = [ 'src' ]
}
}
ERROR:Cannot infer Groovy class path because no Groovy Jar was found on class path: configuration ':compile'
And if I change src line to:
srcDirs = [ 'src/**' ]
It builds but leaves out all my source again.

Related

Is there a way to generate C# code from .proto files with servive(grpc) using Gradle?

I'm trying to generate C# code with gradle. I was able to generate message blocks but service blocks don't seem to be generated.
Here is my gradle file:
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.proto
plugins {
id("java")
id("idea")
id("com.google.protobuf")
}
repositories {
google()
mavenCentral()
}
dependencies {
implementation("com.google.protobuf:protobuf-kotlin:${project.property("protobuf.version")}")
implementation("io.grpc:grpc-kotlin-stub:${project.property("grpc.kotlin.version")}")
implementation("io.grpc:grpc-protobuf:${project.property("grpc.version")}")
}
sourceSets {
main {
proto {
srcDir("src")
}
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:${project.property("protobuf.version")}"
}
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:${project.property("grpc.version")}"
}
id("grpckt") {
artifact = "io.grpc:protoc-gen-grpc-kotlin:${project.property("grpc.kotlin.version")}:jdk8#jar"
}
}
generateProtoTasks {
all().forEach {
ofSourceSet("main")
it.plugins {
id("grpc") {}
id("grpckt") {}
}
it.builtins {
id("kotlin") {}
id("csharp") {}
}
}
}
}
I think I can't use Grpc.Tools nuget package because imports inside .proto files are not found (I think it require to have .proto files inside the C# project, but I still need gradle to generate Kotlin source code).
So, if it's not possible, my only option is to use protoc executable right?
Edit: I added attribute ProtoRoot and now, I can use Grpc.Tools:
<ItemGroup>
<Protobuf Include="..\..\..\..\FanControl\proto\src\proto\settings.proto" GrpcServices="None" ProtoRoot="..\..\..\..\FanControl\proto\src\" />
</ItemGroup>

Import swagger-codegen project into existing Android project

Im trying to integrate a "module"-project generated by swagger-codegen, into my Android project.
Haven't worked that much with gradle before and the swagger-codegen creates a quite messy build.gradle from my point of view.
I have a hard time finding documentation on how to do this. And I feel a bit lost.
I used this method described in the FAQ
mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i http://petstore.swagger.io/v2/swagger.json \
-l java --library=okhttp-gson \
-o /var/tmp/java/okhttp-gson/
So fare I tried to copy the source from the project that was generated by swagger-codegen and merge the two gradle build files. I removed the Junit tests because I couldn't get the Junit dependency working (Implementing Swagger-codegen project - Error:(23, 17) Failed to resolve: junit:junit:4.12). But then I got stuck with some conflict between the plugins?
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Here's the build.gradle:
import static jdk.nashorn.internal.runtime.regexp.joni.ApplyCaseFold.apply
apply plugin: 'idea'
apply plugin: 'eclipse'
group = 'io.swagger'
version = '1.0.0'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// classpath 'com.android.tools.build:gradle:1.5.+'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
repositories {
jcenter()
maven { url 'http://repo1.maven.org/maven2' }
}
if(hasProperty('target') && target == 'android') {
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
// Rename the aar correctly
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "\u0024{project.name}- \u0024{variant.baseName}-\u0024{version}.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
dependencies {
provided 'javax.annotation:jsr250-api:1.0'
}
}
afterEvaluate {
android.libraryVariants.all { variant ->
def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
task.description = "Create jar artifact for ${variant.name}"
task.dependsOn variant.javaCompile
task.from variant.javaCompile.destinationDir
task.destinationDir = project.file("${project.buildDir}/outputs/jar")
task.archiveName = "${project.name}-${variant.baseName}-${version}.jar"
artifacts.add('archives', task);
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
} else {
apply plugin: 'java'
apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
install {
repositories.mavenInstaller {
pom.artifactId = 'XxxxXxxx'
}
}
task execute(type:JavaExec) {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
}
}
dependencies {
compile 'io.swagger:swagger-annotations:1.5.8'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
compile 'com.google.code.gson:gson:2.6.2'
compile 'joda-time:joda-time:2.9.3'
// testCompile 'junit:junit:4.12'
}
Am I doing something complete wrong here? What is the correct way to implement swagger-codegen code into my project?
The swift way to import it was to compile the swagger generated project then copy the .jar file to my android project and add its as a library.
I have a hard time finding documentation on how to do this. And I feel a bit lost.
You could clone the Android swagger-codegen example.
(which does use Junit, so I'm not sure what error you got)
Unless that's what you mean by
So far I tried to copy the source and merge the two gradle build files
To which, I ask, what two Gradle files? It looks like you merged an Android Gradle file with a Java Gradle file, which seems to causing more issues because you are getting...
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Which seems pretty self explanatory when you have this line
apply plugin: 'java'
It's not too clear what you are trying to do here other than check the build target
if(hasProperty('target') && target == 'android')

How to build Groovy JAR w/ Gradle and publish it to in-house repo

I have a Groovy project and am trying to build it with Gradle. First I want a package task that creates a JAR by compiling it against its dependencies. Then I need to generate a Maven POM for that JAR and publish the JAR/POM to an in-house Artifactory repo. The build.gradle:
apply plugin: "groovy"
apply plugin: "maven-publish"
repositories {
maven {
name "artifactory01"
url "http://myartifactory/artifactory/libs-release"
}
}
dependencies {
compile "long list starts here"
}
// Should compile up myapp-<version>.jar
jar {
}
// Should publish myapp-<version>.jar and its (generated) POM to our in-house Maven/Artifactory repo.
publishing {
publications {
myPublication(MavenPublication) {
from components.java
artifact sourceJar {
classifier "source"
}
pom.withXml {
// ???
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
However I do not believe I have set up versioning correctly with my jar task (for instance, how could I get it creating myapp-1.2.1 vs. myapp-1.2.2? I also don't think I have my publications configuration set up correctly: what should go in pom.withXml?
You're more than welcome to use artifactory plugin for that.
The documentation can be found in our user guide and below you can find a full working example of gradle build.
Run gradle build artifactoryPublish to build and publish the project.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '3.0.1')
}
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
group = 'com.jfrog.example'
version = '1.2-SNAPSHOT'
status = 'SNAPSHOT'
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile 'junit:junit:4.11'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
publishing {
publications {
main(MavenPublication) {
from components.java
artifact sourcesJar
}
}
artifactory {
contextUrl = 'http://myartifactory/artifactory'
resolve {
repository {
repoKey = 'libs-release'
}
}
publish {
repository {
repoKey = 'libs-snapshot-local'
username = 'whatever'
password = 'whatever123'
}
defaults {
publications 'main'
}
}
}
package is a keyword in Java/Groovy, and you'd have to use a different syntax to declare a task with that name.
Anyway, the task declaration for package should be removed, as the jar task already serves that purpose. The jar task configuration (jar { from ... }) should be at the outermost level (not nested inside another task), but from configurations.compile is unlikely what you want, as that will include Jars of compile dependencies into the Jar (which regular Java class loaders can't deal with), rather than merging them into the Jar. (Are you even sure you need a fat Jar?)
Likewise, the publish task declaration should be removed, and replaced with publishing { publications { ... } }.
Also, the buildscript block should probably be removed, and repositories { ... } and dependencies { ... } moved to the outermost level. ( buildscript { dependencies { ... } } declares dependencies of the build script itself (e.g. Gradle plugins), not the dependencies of the code to be compiled/run.)
I suggest to check out the many self-contained example builds in the samples directory of the full Gradle distribution (gradle-all).

gradle merge configurations.testRuntime with configurations.testCompile in copy

I'd like to copy some of my dependencies (retrieved from maven) to specific location in my build.gradle file.
If I iterate only testRuntime it works OK. My code is:
dependencies {
testRuntime 'p6spy:p6spy:2.+'
testRuntime 'com.h2database:h2:1+'
}
task foo {
copy {
from configurations.testRuntime.findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
into "outputdir"
}
}
however I'd like to go for testCompile rather than testRuntime in case of h2 dependency. So I tried:
dependencies {
testRuntime 'p6spy:p6spy:2.+'
testCompile 'com.h2database:h2:1+'
}
task foo {
copy {
from [ configurations.testRuntime, configurations.testCompile ].flatten().findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
into "outputdir"
}
}
However here I get error:
No such property: from for class: org.gradle.api.internal.file.copy.CopySpecWrapper_Decorated
I guess trouble is in my merging of the 2 lists here. Still can't figure out the right way.
Your solution will copy the files on every build invocation, even if the foo task isn't invoked. Here is a correct solution:
task foo(type: Copy) {
from configurations.testRuntime // includes configurations.testCompile
into "outputdir"
include "**/p6spy/**"
include "**/h2/**"
}
well, found solution myself, documenting it:
dependencies {
testRuntime 'p6spy:p6spy:2.+'
testCompile 'com.h2database:h2:1+'
}
task foo {
copy {
from configurations.testRuntime.plus(configurations.testCompile).findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
into "outputdir"
}
}

NDK Debugging with gradle-experimental plugin

I'm trying to add native debugging to a project that is an Android Studio NDK project. In the past I just used gradle to kick off a shell script, which built the NDK lib. Now I'm trying to move to use the gradle-experimental plugin.
I've scoured the net for what little info there is, (mostly here, Android Tools Site - Gradle Experimental), about using gradle-experimental with the NDK and I've put together this build.gradle file which is using the preview NDK support for doing the NDK build inline with the Java build.
After finally getting this together from bits-and-pieces of info, I managed to get the NDK portion building, but now it fails to include the httpmime-4.4-beta1.jar file that is clearly included in the dependencies, and I've tried many different permutations of it such as in:
compile files("libs/httpmime-4.4.jar")
But regardless, the errors for the missing symbols from the Jar file still appear.
build.gradle source
apply plugin: 'com.android.model.application'
String APP_PACKAGE_NAME = 'com.obfuscated.app',
VERSION_NAME = '3.0',
TOOLS_VERSION = '23.0.2'
int VERSION_CODE = 15,
MIN_SDK_VERSION = 13,
TARGET_SDK_VERSION = 19,
COMPILE_SDK_VERSION = 23
model {
repositories {
libs(PrebuiltLibraries) {
// prebuilt binaries mirroring Android.mk
libstuff {
headers.srcDirs.add(file("jni/stuff/include/stuff"))
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("jni/stuff/lib/libstuff.so")
}
}
// ...several more of these actually exist in build.gradle and are working
cares {
headers.srcDirs.add(file("jni/c-ares/include"))
binaries.withType(SharedLibraryBinary) {
// StaticLibraryBinary and staticLibraryFile doesnt work despite sample code, at least not for com.android.tools.build:gradle-experimental:0.6.0-alpha5, this builds even though its a static-lib
sharedLibraryFile = file("jni/c-ares/lib/libcaresARM.a")
}
}
}
}
android {
compileSdkVersion = COMPILE_SDK_VERSION
buildToolsVersion = TOOLS_VERSION
defaultConfig.with {
applicationId = APP_PACKAGE_NAME
minSdkVersion.apiLevel = MIN_SDK_VERSION
targetSdkVersion.apiLevel = TARGET_SDK_VERSION
versionCode = VERSION_CODE
versionName = VERSION_NAME
buildConfigFields {
create() {
type "int"
name "VALUE"
value "1"
}
}
compileOptions.with {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
}
signingConfigs {
create("appRelease") {
storeFile file('sign.jks')
storePassword '...'
keyAlias '...'
keyPassword '...'
storeType "jks"
}
}
} // end android
android.lintOptions {
abortOnError = false
}
android.packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
android.ndk {
moduleName = "native"
toolchain "clang"
toolchainVersion "3.5"
platformVersion = MIN_SDK_VERSION
ldLibs.addAll('atomic', 'android', 'log', 'OpenSLES')
abiFilters.addAll(["armeabi", "armeabi-v7a"])
CFlags.addAll(["-mfloat-abi=softfp", "-mfpu=neon", "-O3", "-DCARES_STATICLIB", "-Wno-c++11-long-long"])
cppFlags.addAll(["-I${file("jni")}".toString(),
"-I${file("jni/c-ares/include")}".toString(),
"-I${file("jni/coffeecatch")}".toString()])
stl = "stlport_shared"
}
android.sources {
main {
jniLibs {
dependencies {
}
}
jni {
dependencies {
library "libstuff"
library "cares"
// ...
}
source {
srcDir "jni"
}
}
// java {
// dependencies {
// compile files("libs/httpmime-4.4-beta1.jar")
// compile files("libs/FlurryAnalytics-5.1.0.jar")
// }
// }
}
}
android.buildTypes {
debug {
ndk.with {
debuggable = true
}
}
release {
minifyEnabled = false
ndk.with {
debuggable = true
}
}
}
android.productFlavors {
create("arm") {
ndk.with {
abiFilters.add("armeabi-v7a")
ldLibs.addAll([file("jni/stuff/lib/libstuff.so").toString(),
file("jni/c-ares/lib/libcaresARM.a").toString()])
}
}
create("fat") {
// compile and package all supported ABI
}
}
} // end model
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.+'
compile 'com.android.support:appcompat-v7:23.+'
compile 'com.android.support:support-v13:23.+'
compile 'com.android.support:support-annotations:23.+'
compile 'com.squareup:otto:1.3.8'
compile 'com.github.machinarius:preferencefragment:0.1.1'
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile 'com.fasterxml.jackson.core:jackson-core:2.5.+'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.+'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.+'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.google.guava:guava:19.0'
}
allprojects {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked"
}
}
Out of frustration, I switched back to the non-experimental branch, and even with the old build.gradle file it is now failing to find that same jar file. So is it a problem with Android Studio 2.0 Preview 6?
Has anyone else experienced this, or have a solution for it? It would be so convenient to finally have NDK debugging work right in Android Studio, and if it weren't for this last hurdle, I think I would be there.
Short of re-writing the code that depends on that jar file, I am at a loss for what else to try. I am also open to suggestions for the format of my build.gradle file above, since the documentation for these new features is very sparse still, and some of the samples seem to be already out-of-date with regard to the proper syntax.
WHAT AM I MISSING?
You can see that the C and Cpp (mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainCpp), steps are happening just fine, but then the Javac fails. This jar file approach has worked fine for the past 2 years or so for the http-mime lib from apache, so I don't understand why suddenly this is a problem.
:mobile:mergeArmDebugAndroidTestAssets
:mobile:generateArmDebugAndroidTestResValues UP-TO-DATE
:mobile:generateArmDebugAndroidTestResources
:mobile:mergeArmDebugAndroidTestResources
:mobile:processArmDebugAndroidTestResources
:mobile:generateArmDebugAndroidTestSources
:mobile:copyArmeabi-v7aDebugArmSharedLibraryStlSo
:mobile:compileNativeArmeabi-v7aDebugArmSharedLibraryNativeMainC
:mobile:compileNativeArmeabi-v7aDebugArmSharedLibraryNativeMainCpp
:mobile:linkNativeArmeabi-v7aDebugArmSharedLibrary
:mobile:nativeArmeabi-v7aDebugArmSharedLibrary
:mobile:stripSymbolsArmeabi-v7aDebugArmSharedLibrary
:mobile:ndkBuildArmeabi-v7aDebugArmSharedLibrary
:mobile:ndkBuildArmeabi-v7aDebugArmStaticLibrary UP-TO-DATE
:mobile:copyArmeabiDebugArmSharedLibraryStlSo
:mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainC
:mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainCpp
:mobile:linkNativeArmeabiDebugArmSharedLibrary
:mobile:nativeArmeabiDebugArmSharedLibrary
:mobile:stripSymbolsArmeabiDebugArmSharedLibrary
:mobile:ndkBuildArmeabiDebugArmSharedLibrary
:mobile:ndkBuildArmeabiDebugArmStaticLibrary UP-TO-DATE
:mobile:processAndroidArmDebugMainJniLibs UP-TO-DATE
:mobile:androidArmDebug
:mobile:compileArmDebugJavaWithJavac
:mobile:compileArmDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
Yes, I know the apache libs are deprecated, but this is legacy code that should work despite that fact, and will be updated in the future.
A general way to do the include you're looking for is this in the dependencies.
compile fileTree(dir: 'libs', include: ['*.jar'])
However, I'm not certain that will solve this particular problem. I've always had success with putting the jar in the libs directory at the top of the directory structure.
If you need to have the jar in a different location, then this works for me:
repositories {
flatDir {
dirs '<relativePathToJar>'
}
}
model { ... }
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

Resources