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
Related
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'
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
i'm trying to add compile method to build.gradle but i get this problem
i have android studio 3.5.2
and 5.4.1 gradle
You can replace compile with implementation:
implementation 'com.android.support:appcompat-v7:24.1.1'
implementation 'com.android.support:design:24.1.1'
But you are putting your dependencies in your build.gradle module Project. Place them in build.gradle module app.
Excluding a transitive dependency in Gradle is pretty straightforward:
compile('com.example.m:m:1.0') {
exclude group: 'org.unwanted', module: 'x'
}
How would we go around he situation in which we use a plugin:
apply: "somePlugin"
And when getting the dependencies we realize that the plugin is bringing some transitive dependencies of its own?
You can remove dependencies after the plugin is applied, (from a single configuration, or to all configurations) using eg. compile.exclude. Note that compile resolves to a "Configuration"; see the javadocs at Configuration.exclude .
edit
Be aware that excluding dependecies could fail, if the configuration has already been resolved.
Sample script
apply plugin: 'java-library'
repositories {
jcenter()
}
dependencies {
compile 'junit:junit:4.12'
compile 'ant:ant:1.6'
compile 'org.apache.commons:commons-lang3:3.8'
}
// remove dependencies
configurations.all {
exclude group:'junit', module:'junit'
}
configurations.compile {
exclude group:'org.apache.commons', module: 'commons-lang3'
}
println 'compile deps:\n' + configurations.compile.asPath
You can manipulate the classpath of the buildscript itself through:
buildscript {
configurations {
classpath {
exclude group: 'org', module: 'foo' // For a global exclude
}
}
dependencies {
classpath('org:bar:1.0') {
exclude group: 'org', module: 'baz' // For excluding baz from bar but not if brought elsewhere
}
}
}
Here is another way to enforce your project to strictly use a specific version for the build.gradle.kts
val grpcVersion = "1.45.1"
implementation("io.grpc:grpc-stub") {
version {
strictly(grpcVersion)
}
}
More info can be found at the gradle documentation: https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html
I have a fairly simple setup, that does not work and I cannot work out why:
the folder structure is as follows:
/dependency
/build.gradle
/settings.gradle
/src/main/...
/Mainproject
/build.gradle
/settings.gradle
/Subproject_1
/build.gradle
/src/main...
/Subproject_2
/build.gradle
/src/main...
I want the Subproject to depend on "dependency" locally for quick testing.
so I stuck to the manual and did:
/Mainproject/settings.gradle:
include "Subproject_1", "Subproject_2", "dependency"
project(":dependency").projectDir = file('../dependency')
/Mainproject/build.gradle:
allprojects {
apply plugin: 'java'
dependencies {
compile project(path: ':dependency')
}
}
dependencies {
compile project(':Subproject_1')
compile project(':Subproject_2')
}
/dependency/build.gradle:
version '1.0'
apply plugin: 'java'
repositories {
maven {
url "http://...."
}
}
dependencies {
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
compile group: 'javax', name: 'javaee-api', version: '7.0'
}
jar {
manifest {
attributes 'Implementation-Title': 'Archive delegation dispatcher classes',
'Implementation-Version': project.version
}
}
The build.gradle files of Subproject_1 and _2 are empty.
The settings.gradle file of dependency is empty.
When i gradle build MainProject i get:
Circular dependency between the following tasks:
:dependency:classes
\--- :dependency:compileJava
\--- :dependency:jar
\--- :dependency:classes (*)
(*) - details omitted (listed previously)
And I cannot get my head around why that would be.
Any hints?
When you include "dependency" in your Mainproject's settings.gradle file, you are making the "dependency" project a subproject of "Mainproject".
Then this block in your Mainproject's build.gradle file defines "dependency" as a compile dependency of all subprojects.
allProjects {
dependencies {
compile project(path: ':dependency')
}
}
Since the "dependency" project is also a subproject, you have a circular dependency defined where the "dependency" project depends on itself.
Instead, try creating a settings.gradle file for each of the subprojects with the following:
include "dependency"
project(":dependency").projectDir = file('../dependency')
Then modify your settings.gradle file for the Mainproject to look like this:
include "Subproject_1", "Subproject_2"
You've stated that allProjects have a dependency on the project dependency as seen here:
allprojects {
apply plugin: 'java'
dependencies {
compile project(path: ':dependency')
}
}
You need this to only apply to your projects that aren't dependency. You can do that by excluding it when applying dependencies, like this
subprojects { project ->
if (project.name != "dependency") {
apply plugin: 'java'
dependencies {
compile project(path: ':dependency')
}
}
}
Because dependencies itself.
Move it must be OK.
Turning off Instant run worked for me, could work for someone else too. I had some changes related to gradle and my application appeared not to work after it.
build.gradle (:shared)
This is because you are trying to include a module inside of yourself.
From:
dependencies {
implementation project(':shared')
...
}
To:
dependencies {
...
}
GL
Source
I have a java EE project. I am using gradle as a build tool. My build.gradle file looks as follows:
apply plugin:'war'
apply plugin:'eclipse'
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "com.eriwen:gradle-js-plugin:1.12.1"
}
}
apply plugin: "com.eriwen.gradle.js"
minifyJs {
source = file("src/main/webapp/js/App.js")
dest = file("build/all-min.js")
closure {
warningLevel = 'QUIET'
}
}
war.doFirst {
tasks.minifyJs.execute()
}
war.webInf {
from "build/all-min.js"
into "/js/"
}
dependencies{
compile 'org.springframework:spring-context:4.1.4.RELEASE'
compile 'org.springframework:spring-core:4.1.4.RELEASE'
compile 'org.springframework:spring-web:4.1.4.RELEASE'
compile 'org.springframework:spring-webmvc:4.1.4.RELEASE'
compile 'org.springframework:spring-jdbc:4.1.4.RELEASE'
compile 'org.springframework:spring-tx:4.1.4.RELEASE'
compile 'org.google.code.gson:gson:2.3.1+'
compile 'log4j:log4j:1.2.17+'
compile 'org.slf4j:jcl-over-slf4j:1.5.8+'
compile 'org.slf4j:slf4j-api:1.5.8+'
compile 'org.slf4j:slf4j-log4j12:1.5.8+'
compile 'org.apache.commons:commons-dbcp2:2.0.1+'
compile 'org.mybatis:mybatis-spring:1.2.2+'
compile 'org.mybatis:mybatis:3.2.8+'
compile 'com.oracle:ojdbc14:10.2.0.4.0+'
compile 'commons-fileupload:commons-fileupload:1.2.1+'
compile 'commons-io:commons-io:2.4+'
compile 'junit:junit:4.11'
compile 'javax.servlet:servlet-api:2.5'
}
However when I try to execute gradle build I get the following error
couldnot resolve all dependencies required for configuration for all the dependencies that I have mentioned in my build.gradle file. I think I have correctly mentioned the repositories . I cant figure out why my build is failing. Any suggestion would be appreciated.
You need to add a repositories {...} configuration block to your build script. The one declared inside the buildscript {...} block is only used for resolving dependencies of the build script itself (in this case, the javascript plugin), but it is not used for resolving project dependencies.