gradle not using compile time dependencies when building - build

Hi guys I'm new to gradle and have the following issue.
When I use the java plugin and invoke $gradle build on my project it does not put my third party dependencies on the classpath. My build.gradle file looks as follows:
apply plugin: 'java'
sourceSets.main.java.srcDirs = ["src/main/java", "src/main/web"]
repositories {
flatDir name: 'thirdParty', dirs: 'C:/dev/repo'
}
dependencies {
compile files('log4j-1.2.12.jar', 'gson-1.7.1.jar')
}
and the error output from gradle is the following
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:3: package com.google.gson does not exist
import com.google.gson.Gson;
^
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:7: cannot find symbol
symbol : class Gson
location: class org.gradle.example.simple.HelloWorld2
Gson gson = new Gson();
^
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:7: cannot find symbol
symbol : class Gson
location: class org.gradle.example.simple.HelloWorld2
Gson gson = new Gson();
I have stated where my repo jars live and told it that when it compiles it must include the above mentioned jars.
Please help.

Your dependency declarations are incorrect. You can either specify a flatDir repo and use the usual dependency syntax for external dependencies (group:module:version), or use the files method together with correct file paths (then you don't need to declare a repository). In the former case, you may leave out the dependency's group. For example:
repositories {
flatDir name: 'thirdParty', dirs: 'C:/dev/repo'
}
dependencies {
compile ':log4j:1.2.12'
}
For more information, consult the Gradle User Guide.

Related

Gluon build as single Jar

I'd like to build my Gluon Project as a SINGLE executable jar file.
Currently there is a startup script included with a bin folder and so on.
Is it possible to build a single jar? Or can I include an own gradle task that accomplishes this?
The current tasks like installApp or distZip will not bundle the jars in one.
If you want to create a 'fat-jar', you can add this plugin to your build.gradle script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}
apply plugin: 'org.javafxports.jfxmobile'
apply plugin: 'com.github.johnrengelman.shadow'
And then run gradlew shadowJar. It will create under libs an executable jar.
Note: if you are using desktopCompile or desktopRuntime dependencies, those won't be included so you need to change those to compile or runtime.
Since I had to build the jar with javapackager I used this plugin: javafx-gradle-plugin
I am relatively new to Gradle so adding the dependencies is just a temporary solution but it works
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.4.1'
}
}
jfx {
verbose = true
mainClass = "package.path.to.main.class.MainClass"
jfxAppOutputDir = "build/jfx/app" //configurable
jfxMainAppJarName = "ProjectName.jar"
deployDir = "src/main/deploy" //for signing
//many more options, go to link to learn about them
}
jfxJar.doFirst {
//TODO add all desktop dependencies
println("adding desktop dependency")
project.dependencies.add("runtime", "com.gluonhq:charm-desktop:2.1.0")
}

How to reference external library dependencies in qt qbs?

Let's say I've downloaded some library xyz with headers and binaries and put it somewhere not in standard search paths. For each Product I can add the search paths and library to link to cpp.includePaths, cpp.libraryPaths, cpp.staticLibraries, etc.
Is there a better [standard] way to do this? If I'm building the library as part of my project it seems I can define the paths in an Exports item and then use a Depends item in each Product to automatically set the paths. This seems like a nice mechanism and I wonder if there isn't a way to use it for external dependencies as well.
The qbs docs are a little thin...
Thanks!
You would typically create your own module for xyz. You can add locations where QBS would search for modules and imports by setting the project's qbsSearchPaths-property. E.g. by setting it to "qbs" QBS would search for additional modules in the "qbs/modules" sub-directory of your project.
There you could place a file called "xyz.qbs" that would look like this:
import qbs
Module {
Depends { name: "cpp" }
property string xyzPath: "the/xyz/path"
cpp.includePaths: xyzPath + "/include"
cpp.libraryPath: xyzPath + "/lib"
cpp.staticLibraries: "xyz"
}
You could then use it by simply adding a Depend to your project:
import qbs
Project {
qbsSearchPaths: "qbs"
CppApplication {
name: "myApp"
files: "src/**"
Depends { name: "xyz" }
}
}

Gradle Multiproject build dependencies: package does not exist

My project structure
Main_Project/
--build.gradle
--settings.gradle
--com.project.core/
----build.gradle
--com.project.core.test/
----build.gradle <--this requires classes defined in ProjectCore
Dependencies for ProjectTest build.gradle:
dependencies {
compile project(':com.project.core')
testCompile "junit:junit:4"
runtime files('C:/eclipse/plugins/org.hamcrest.core_1.3.0.v201303031735.jar')
}
Here is my settings.gradle:
include 'com.project.core',
'com.project.core.test'
Reported error message:
H:\gitwork\com.project.core.test\src\com\project\common\providable\sortedprovidablemanagertest\SimpleObject.java:8: error:
package com.project.core.providable.abstractions does not exist
import com.project.core.providable.abstractions.AProvidable;
If you want to use project names that differ from the corresponding directory names, you'll have to configure the latter separately:
include 'com.project.core'
include 'com.project.core.test'
project(':com.project.core').projectDir = 'ProjectCore'
project(':com.project.core.test').projectDir = 'ProjectTest'

Why doesn't Gradle include transitive dependencies in compile / runtime classpath?

I'm learning how Gradle works, and I can't understand how it resolves a project transitive dependencies.
For now, I have two projects :
projectA : which has a couple of dependencies on external libraries
projectB : which has only one dependency on projectA
No matter how I try, when I build projectB, gradle doesn't include any projectA dependencies (X and Y) in projectB's compile or runtime classpath. I've only managed to make it work by including projectA's dependencies in projectB's build script, which, in my opinion does not make any sense. These dependencies should be automatically attached to projectB. I'm pretty sure I'm missing something but I can't figure out what.
I've read about "lib dependencies", but it seems to apply only to local projects like described here, not on external dependencies.
Here is the build.gradle I use in the root project (the one that contains both projectA and projectB) :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.3'
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
group = 'com.company'
repositories {
mavenCentral()
add(new org.apache.ivy.plugins.resolver.SshResolver()) {
name = 'customRepo'
addIvyPattern "ssh://.../repository/[organization]/[module]/[revision]/[module].xml"
addArtifactPattern "ssh://.../[organization]/[module]/[revision]/[module](-[classifier]).[ext]"
}
}
sourceSets {
main {
java {
srcDir 'src/'
}
}
}
idea.module { downloadSources = true }
// task that create sources jar
task sourceJar(type: Jar) {
from sourceSets.main.java
classifier 'sources'
}
// Publishing configuration
uploadArchives {
repositories {
add project.repositories.customRepo
}
}
artifacts {
archives(sourceJar) {
name "$name-sources"
type 'source'
builtBy sourceJar
}
}
}
This one concerns projectA only :
version = '1.0'
dependencies {
compile 'com.company:X:1.0'
compile 'com.company:B:1.0'
}
And this is the one used by projectB :
version = '1.0'
dependencies {
compile ('com.company:projectA:1.0') {
transitive = true
}
}
Thank you in advance for any help, and please, apologize me for my bad English.
I know that this specific version of the question has already been solved, but my searching brought me here and I hope I can save some people the hassle of figuring this out.
Bad foo/build.gradle
dependencies {
implementation 'com.example:widget:1.0.0'
}
Good foo/build.gradle
dependencies {
api 'com.example:widget:1.0.0'
}
bar/build.gradle
dependencies {
implementation project(path: ':foo')
}
implementation hides the widget dependency.
api makes the widget dependency transitive.
From https://stackoverflow.com/a/44493379/68086:
From the Gradle documentation:
dependencies {
api 'commons-httpclient:commons-httpclient:3.1'
implementation 'org.apache.commons:commons-lang3:3.5'
}
Dependencies appearing in the api configurations will be
transitively exposed to consumers of the library, and as such will
appear on the compile classpath of consumers.
Dependencies found in the implementation configuration will, on the
other hand, not be exposed to consumers, and therefore not leak into
the consumers' compile classpath. This comes with several benefits:
dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive
dependency
faster compilation thanks to reduced classpath size
less recompilations when implementation dependencies change: consumers would not need to be recompiled
cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that
distinguish exactly between what is required to compile against the
library and what is required to use the library at runtime (in other
words, don't mix what is needed to compile the library itself and what
is needed to compile against the library).
The compile configuration still exists, but should not be used as it will not offer the guarantees that the api and implementation
configurations provide.
Finally, the problem didn't come from the scripts. I've just cleared gradle's cache, and each project's build folder, to make this work.
Put the following line in projectB's dependencies.
compile project(':projectA')

Gradle: How to make a compile scope file dependency excluded in packaging?

I have a multi-module gradle project with the following basic structure:
root
core
c-interface
m-interface
The c-interface and m-interface both depend on the core project:
compile project(':root:core')
c-interface and m-interface use the WAR plugin, but core does not and is just a jar.
In the core project, I am pulling in some file system dependencies with the following. One of these dependencies I cannot have packaged in the WARs generated by c-interface and m-interface. Previously I had this dependency in a nexus maven repository so I could exclude it by group,name,version in a providedRuntime configuration in c-interface and m-interface.
I cannot figure out how to do the same for the file dependency. The gradle dependencies task does not list file dependencies so I don't know what I would put in a providedRuntime.
I read http://issues.gradle.org/browse/GRADLE-471 but trying to use the idea there doesn't seem to remove the archive from my packages. Here is what I am currently defining (in core's build.gradle):
compile fileTree(dir: 'dependencies/compile/archive', include: '*.jar', exclude: 'management.jar')
compile(files('dependencies/compile/archive/management.jar')){ notPackaged = true } // Excludes it from all publications
Update
providedCompile without war plugin looked like a possibility. I set this up in the core build.gradle and it compiled fine, but c-interface and m-interface also needed the dependency at compile time. Including the file as providedCompile (or even a sanity check with compile) in c-interface and m-interface did not fix compile time errors related to missing the management.jar dependency. My speculation is because it was already scoped as providedCompile in core that the new declarations are ignored in c-interface and m-interface.
core/build.gradle:
configurations { providedCompile }
dependencies {
providedCompile files('dependencies/compile/archive/management.jar')
}
sourceSets.main.compileClasspath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile
c-interface/build.gradle:
providedCompile files('dependencies/compile/archive/management.jar')
There probably is a cleaner and simpler solution but you could then specify a custom configuration:
configurations {
compileOnly
}
and then specify all dependencies:
dependencies {
compile fileTree(dir: 'dependencies/compile/archive', include: '*.jar', exclude: 'management.jar')
compileOnly files('dependencies/compile/archive/management.jar')
}
Finally add the compileOnly configuration to classpaths of all source sets
sourceSets.all {
compileClasspath += configurations.compileOnly
}
This way management.jar should be on the classpath for compilation but won't be packaged.
EDIT
Only now I fully understand your problem. The following worked for me on a test project.
In core project gradle file:
repositories {
flatDir {
dirs 'dependencies/compile/archive'
}
}
dependencies {
compile fileTree(dir: 'dependencies/compile/archive', include: '*.jar', exclude: 'management.jar')
compile ':management:'
}
In project that depends on core:
repositories {
flatDir {
dirs new File(project(':core').projectDir, 'dependencies/compile/archive')
}
}
dependencies {
compile(project(':core')) {
exclude module: 'management'
}
compileOnly ':management':
}
sourceSets.all {
compileClasspath += configurations.compileOnly
}