I have a project with 1 subproject:
projroot/
settings.gradle
build.gradle
mod1/
build.gradle
In mod1/build.gradle there is a "provided" dependency:
apply plugin: 'java'
configurations {
provided
}
repositories {
mavenCentral()
}
dependencies {
provided group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}
This doesn't seem to work when mod1/src/main/java/One.java imports javax.servlet.ServletContext:
> Task :mod1:compileJava FAILED
/home/x/groot/mod1/src/main/java/One.java:1: error: package javax.servlet does not exist
import javax.servlet.ServletContext;
^
1 error
...
BUILD FAILED in 0s
2 actionable tasks: 1 executed, 1 up-to-date
Using gradle-4.4.1.
Am I defining the dependency in an incorrect place?
Since Gradle 2.12 introduced compileOnly dependencies I rather recommend to use this. Especially when it comes to a Servlet API that will be provided by the servlet container once you deploy your web application on e.g. Tomcat, JBoss, ....
dependencies {
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}
provided is a scope used in Maven project that haven't found place in Gradle so far. But compileOnly is very close to Mavens provided scope.
Related
I have a multi module project in IntelliJ, a java library project, using Gradle with quite old versions of libraries
I must say, and now switching to Java 11 (using right now OpenJDK 11.0.2 from https://jdk.java.net/archive/).
I want to modularize that library project, adding to all modules a module-info.java.
I keep getting an error in one of the modules with one of the dependencies, Saxon-HE.
I isolated that module in a separate project (using Gradle 7.6), and modified the build.gradle dependencies step by step as IntelliJ discovered
import errors, while using the latest versions of the dependencies.
The build.gradle of the project looks like this:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The build.gradle of the module looks like this up to the point with the error with Saxon-HE.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
implementation group: 'org.jdom', name: 'jdom2', version: '2.0.6.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.6'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.10.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The module-info.java looks like this, I used the recommendations of IntelliJ so it added automatically the requires statements:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
}
The next import error IntelliJ discovers while building it results because one of my classes has the import statement:
import net.sf.saxon.xpath.XPathFactoryImpl;
Building the project results in the error:
error: package net.sf.saxon.xpath does not exist
import net.sf.saxon.xpath.XPathFactoryImpl;
Looking at https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE/11.4 I added to build.gradle of the module:
implementation group: 'net.sf.saxon', name: 'Saxon-HE', version: '11.4'
In IntelliJ I can see in the Project navigator view, in External Libraries that there is
Gradle: net.sf.saxon.Saxon-HE:11.4
Saxon-HE-11.4.jar
IntelliJ recommends
Add 'requires Saxon.HE' directive to module-info.java
The module-info.java looks now like this:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
requires Saxon.HE;
}
After that the error in the particular class using that import statement is gone, IntelliJ doesn't complain.
But when then building the project I get the error
C:\Users\ME\PROJECTS\myproject\mymodule1\src\main\java\module-info.java:9: error: module not found: Saxon.HE
requires Saxon.HE;
^
Removing the requires Saxon.HE and building the project results in the error:
error: package net.sf.saxon.xpath is not visible
import net.sf.saxon.xpath.XPathFactoryImpl;
^
(package net.sf.saxon.xpath is declared in the unnamed module, but module net.sf.saxon.xpath does not read it)
I find this error message weired, because it says but module net.sf.saxon.xpath does not read it, I would rather expect but module mymodule1 does not read it.
I don't know what's going wrong, other external dependencies are not problematic but Saxon-HE is.
I found here Gradle build - add module path a snippet which might solved it, but maybe only partially, so not sure if this answer can be marked as the solution.
I added to the project build.gradle
subprojects {
apply plugin: 'java'
sourceCompatibility = 19
targetCompatibility = 19
compileJava {
doFirst {
options.compilerArgs += [
'--module-path', classpath.asPath,
'-Xmaxerrs', 1000
]
classpath = files()
}
}
}
Now trying to build it (with Gradle 7.6 and OpenJDK 19), it doesn't complain to not finding modules, so far at least, but now I have the next problem which I desribed here Java Modules, Gradle, external dependencies - Modules reading from more then one
Whenever i am trying to add dependency in the project through build.gradle , it is not able to add dependency. Instead it throws warning as
Could not resolve: junit:junit:4.12
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:command_line_warnings
CONFIGURE SUCCESSFUL in 1s
I have tried every hook to resolve it, but was not able to.
Please find below build.gradle file which i am using
apply plugin: 'java'
apply plugin: 'idea'
group 'practice'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
As per mvnrepository.com, for Gradle it has been mentioned as
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.12'
The link is given below.
https://mvnrepository.com/artifact/junit/junit/4.12
If it does not work, try to add the repository as given below.
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
junit version 12 also available in maven repo, you can check following link.
http://repo1.maven.org/maven2/junit/junit/4.12/
Sometimes, we have seen sporadic issue because of corrupt file.
You can delete the .gradle file and you can rerun with command like gradle clean build.
I'm building a SpringBoot-based project that will include Apache MetaModel. SpringBoot is version 1.5.8.
Starting up a SpringBoot app works fine, and I can get to published pages. At the moment, the project is pretty much a skeleton app with only very basic features.
However, as soon as I add:
compile 'org.apache.metamodel:MetaModel-full:5.0.0'
to my Gradle dependencies, startup fails with the notorious NoClassDefFound exception for FutureFallback. No other code added, and no configuration added or modified: reproducing the problem simply requires including the compile declaration to cause the startup failure.
I've tried including various versions of Guava and of MetaModel itself, as recommended by other answers, but none of them change the startup failure.
My gradle.build is currently thus:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
}
}
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'
repositories {
mavenCentral()
}
dependencies {
compile project(':commons')
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
compile 'org.apache.metamodel:MetaModel-full:5.0.0'
compile 'mysql:mysql-connector-java:8.0.8-dmr'
compile 'com.microsoft.sqlserver:mssql-jdbc:6.3.4.jre8-preview'
compile 'org.reflections:reflections:0.9.11'
compile 'com.datastax.cassandra:cassandra-driver-core:3.3.1'
// compile 'com.google.guava:guava:23.4-jre'
compile 'com.google.guava:guava:20.0'
testCompile (
'org.junit.jupiter:junit-jupiter-api:5.0.1',
'org.mockito:mockito-core:2.10.0'
)
testRuntime(
'org.junit.jupiter:junit-jupiter-engine:5.0.1',
'org.mockito:mockito-core:2.10.0'
)
}
Evidence of tinkering with Guava versions is evident. Versions 16, 19, 20 and 23.4-jre have been tried.
Ideas for other avenues of repair greatly appreciated.
I am trying to setup Google protobuf with netty, but when I start compilation gradle first download google protobuf (at least at the first attempt) but then at compilation it just tells me :
/src/main/java/GameMoveOuterClass.java:1536: error: package com.google.protobuf.GeneratedMessageV3 does not exist
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
here is my build.gradle :
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
repositories {
mavenCentral()
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
dependencies {
compile group: 'io.netty', name: 'netty-all', version: '4.1.5.Final'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '2.4.1'
}
jar {
manifest {
attributes("Main-Class": 'server.Server',
"Class-Path": configurations.compile.collect { it.getPath() }.join(' '))
}
}
If someone knows what's wrong, please let me know
Thanks
You're using the version 2.4.1 of protobuf which doesn't come with GeneratedMessageV3.
Update to a new version of protobuf which include this class like the 3.0.0
dependencies {
compile group: 'io.netty', name: 'netty-all', version: '4.1.5.Final'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.0.0'
}
Using the maven central advanced search for com.google.protobuf.GeneratedMessageV3 it seems that the class is in com.google.cloud:google-cloud-nio:xxx or maybe com.trueaccord.scalapb:protobuf-runtime-scala_yyy:zzz. I'm guessing you'll need to add one of these to your classpath.
I am not familiar with Gradle but it looks to me like you are mixing new protobuf generated code with an older protobuf library, which is not supported. The GeneratedMessageV3 class was added only recently (around 3.0 I believe), and so new generated code that references that class cannot be linked against an older library which does not include it.
In my scenario, both my app and library module should add
implementation 'com.google.protobuf:protobuf-javalite:3.9.1'
even app has a dependency on library
I have problems with configure my build.gradle for download jar and pom from svn repository. For example, i have url:
https://svn.code.sf.net/p/springframework/svn/repos/repo-ext/javax/xml/crypto/xmldsig/1.0/
and i want did like this compile group: 'com.sun.xml.wss', name: 'xws-security', version: '3.0'
Also, manual download is wrong way.
UPD this is build.gradle file of backend project
apply plugin: 'java'
dependencies {
compile group: 'org.glassfish.metro', name: 'wssx-api', version: '2.1.1-b09'
compile group: 'com.sun.xml.wss', name: 'xws-security', version: '3.0'
compile project(':pp-backend')
}
If you're not downloading from maven central (which is configured in Gradle by default), you should configure the repository to download from using the 'repositories' closure:
repositories {
mavenCentral()
maven {
url 'https://svn.code.sf.net/p/springframework/svn/repos/repo-ext/'
}
}
Then, in the 'dependencies' closure, just add:
compile group: 'javax.xml.crypto', name: 'xmldsig', version: '1.0'
See more info in Dependency Management Basics