compileJava doesn't work with local libraries - java

I'm newbie with Gradle and i'm starting to apply it to an existing project.
What i would like to do is compile java classes using the compileJava task of the java plugin.
I followed the documentation to add all of the jars contained to my local directory but it doesn't seem to work.
When i run the compileJava task it continues to print messages like
"javax.servlet.http does not exist"
"javax.servlet does not exist"
etc.
etc.
It prints messages as above for every package contained in my local folder and even if i try to print dependencies using the 'gradle dependencies' command it prints:
annotationProcessor - Annotation processors and their dependencies for
source set 'main'. No dependencies
apiElements - API elements for main. (n) No dependencies
archives - Configuration for archive artifacts. No dependencies
compile - Dependencies for source set 'main' (deprecated, use
'implementation ' instead). No dependencies
compileClasspath - Compile classpath for source set 'main'. No
dependencies
compileOnly - Compile only dependencies for source set 'main'. No
dependencies
default - Configuration for default artifacts. No dependencies
implementation - Implementation only dependencies for source set
'main'. (n) No dependencies
runtime - Runtime dependencies for source set 'main' (deprecated, use
'runtimeOnly ' instead). No dependencies
runtimeClasspath - Runtime classpath of source set 'main'. No
dependencies
runtimeElements - Elements of runtime for main. (n) No dependencies
runtimeOnly - Runtime only dependencies for source set 'main'. (n) No
dependencies
testAnnotationProcessor - Annotation processors and their dependencies
for source set 'test'. No dependencies
testCompile - Dependencies for source set 'test' (deprecated, use
'testImplementation ' instead). No dependencies
testCompileClasspath - Compile classpath for source set 'test'. No
dependencies
testCompileOnly - Compile only dependencies for source set 'test'. No
dependencies
testImplementation - Implementation only dependencies for source set
'test'. (n) No dependencies
testRuntime - Runtime dependencies for source set 'test' (deprecated,
use 'testRuntimeOnly ' instead). No dependencies
testRuntimeClasspath - Runtime classpath of source set 'test'. No
dependencies
testRuntimeOnly - Runtime only dependencies for source set 'test'. (n)
No dependencies
So...it can't get the dependencies properly.
I've tryied to get one dependency from a remote repository as mavencentral() or jcenter and everything works fine.
Can someone help me? Is there something that i'm making in the wrong way? It seems that fileTree is not working fine.
This is my gradle.build file:
plugins {
id 'java'
}
//Source and target compatibility.
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
//Libraries local repository.
repositories {
flatDir {
dirs 'src/main/webapp/WEB-INF/lib'
}
}
dependencies {
compile fileTree(dir:'src/main/webapp/WEB-INF/lib', include: ['*.jar'])
}
//Source sets.
sourceSets {
//Thes configuration under 'main' will be used for the production environment.
main {
java {
//Source directory of the java files.
srcDir 'src/main/webapp/WEB-INF/classes'
}
//.class files output directory
java.outputDir = file('src/main/webapp/WEB-INF/classes')
}
}

Related

How do I implement "application run" task in Gradle?

I'm using Gradle and I wonder how I can implement a run task so I can run the program from the command "./gradlew run". I have a project named "demo" and it have the task "application run". Then I created the project "HouseObserver" and it have not the task "application run".
My build.gradle file looks like this.
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
apply plugin: 'java'
task runApp(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'HouseObserver.Main'
}
// Using and creating an Executable Jar
jar {
manifest {
attributes('Main-Class': 'HouseObserver.Main')
}
}
task runExecutableJar(type: JavaExec) {
// Executable jars can have only _one_ jar on the classpath.
classpath = files(tasks.jar)
// 'main' does not need to be specified
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// Need at least basic JME
compile "org.jmonkeyengine:jme3-core:3.3.0-beta1"
compile "org.jmonkeyengine:jme3-desktop:3.3.0-beta1"
compile "org.jmonkeyengine:jme3-lwjgl3:3.3.0-beta1"
compile group: 'com.simsilica', name: 'lemur', version: '1.13.0'
compile group: 'com.simsilica', name: 'lemur-proto', version: '1.11.0'
// needed for the style language
runtime "org.codehaus.groovy:groovy-all:2.4.5"
// Standard utility stuff
compile 'com.google.guava:guava:19.0'
compile 'org.slf4j:slf4j-api:1.7.13'
runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
runtime 'org.apache.logging.log4j:log4j-core:2.5'
}
I'm also trying the way to implement a task from a plugin.
plugins {
id 'application'
}
application {
mainClassName = 'my.packages.to.the.Main'
}
But nothing happens. Why?
EDIT:
Here is my latest gradle file.
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id 'application'
}
application {
mainClassName = 'HouseObserver.Main'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// Need at least basic JME
compile "org.jmonkeyengine:jme3-core:3.3.0-beta1"
compile "org.jmonkeyengine:jme3-desktop:3.3.0-beta1"
compile "org.jmonkeyengine:jme3-lwjgl3:3.3.0-beta1"
compile group: 'com.simsilica', name: 'lemur', version: '1.13.0'
compile group: 'com.simsilica', name: 'lemur-proto', version: '1.11.0'
// needed for the style language
runtime "org.codehaus.groovy:groovy-all:2.4.5"
// Standard utility stuff
compile 'com.google.guava:guava:19.0'
compile 'org.slf4j:slf4j-api:1.7.13'
runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
runtime 'org.apache.logging.log4j:log4j-core:2.5'
}

build.gradle: compile group vs compile, buildscript, classpath

What is the difference between "compile group" and "compile"? Just another way to define a dependency?
Ex:
compile group: 'org.slf4j', name: 'slf4j-jcl', version: '1.7.21'
And i think this also will work:
compile("org.slf4j:slf4j-jcl:1.7.21")
Why do i have the declare mavenCentral() again and another dependencies block inside the buildscript block?
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
From my point of view, when you compile something it will be in your classPath?
compile specifies an external dependency for the project you are building. compile requires group, name, and version. These can either be broken out or specified using the short form "group:name:version". see Gradle Dependency Management Basics
The buildscript block declares the dependencies of your gradle build itself while the normal dependencies block declares the dependencies of the project you are going to build

Why doesn't gradle return dependencies for another projects?

I have the following build script (webapp:build.gradle):
apply plugin: 'java'
dependencies {
project(':api')
}
When I run gradle dependencies webapp:dependencies in command line I get:
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
No configurations
:webapp:dependencies
------------------------------------------------------------
Project :webapp
------------------------------------------------------------
archives - Configuration for archive artifacts.
No dependencies
compile - Compile classpath for source set 'main'.
No dependencies
default - Configuration for default artifacts.
No dependencies
runtime - Runtime classpath for source set 'main'.
No dependencies
testCompile - Compile classpath for source set 'test'.
No dependencies
testRuntime - Runtime classpath for source set 'test'.
No dependencies
There was nothing to be said about dependency from api project. Why? What's wrong?
Because You haven't specified configuration name for this dependency. It should be e.g.:
dependencies {
compile project(':api')
}
webapp/build.gradle
apply plugin: 'java'
dependencies {
compile project(':api')
}
webapp/settings.gradle
include 'api'
webapp/api/build.gradle
apply plugin: 'java'

Gradle dependencies local jar file

I have problem with include local jar file.
I have two projects:
ProjectA
ProjectB
--libs
----jgrapht-jdk1.6.jar
My build file include (This is the part that concerns projectB):
project(':ProjectB') {
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
compileOnly
}
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
dependencies {
compileOnly files('libs/jgrapht-jdk1.6.jar')
compile 'org.simpleframework:simple-xml:2.7#jar'
}}
I run command gradlew -q dependencies and see result
Project :ProjectB
archives - Configuration for archive artifacts.
No dependencies
compile - Compile classpath for source set 'main'.
\--- org.simpleframework:simple-xml:2.7
compileOnly
No dependencies
default - Configuration for default artifacts.
\--- org.simpleframework:simple-xml:2.7
runtime - Runtime classpath for source set 'main'.
\--- org.simpleframework:simple-xml:2.7
testCompile - Compile classpath for source set 'test'.
\--- org.simpleframework:simple-xml:2.7
testRuntime - Runtime classpath for source set 'test'.
\--- org.simpleframework:simple-xml:2.7
Line compile 'org.simpleframework:simple-xml:2.7#jar' - working fine,
but line compileOnly files('libs/jgrapht-jdk1.6.jar') don't work.
I use gradle 1.8, Windows 7 x64
Tell me please where I made a mistake.
It's a known limitation that file dependencies aren't currently shown by gradle dependencies. They nevertheless work (if the paths are right).

Gradle local dependencies are not visible

In my project I use some local dependencies:
dependencies {
compile files('lib/mylib.jar')
}
Why when I call gradle dependencies I can't see this library as a dependency? Command gradle dependencies --configuration compile returns this:
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
compile - Compile classpath for source set 'main'.
No dependencies
Dependencies downloaded from repository (maven/ivy) are visible. For example:
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:14.0.1'
}
will show:
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
compile - Compile classpath for source set 'main'.
\--- com.google.guava:guava:14.0.1
BUILD SUCCESSFUL
I should also add that dependencies are not shown but project compiles properly.
Gradle documentation on file dependency explains
File dependencies are not included in the published dependency descriptor for your project. However, file dependencies are included in transitive project dependencies within the same build. This means they cannot be used outside the current build, but they can be used with the same build.
Use api(name: 'mylib', ext: 'jar') as a workaround.

Categories

Resources