Gradle war fails with compile task - java

In order to set war file into Tomcat server, I try :
Gradle war (5.3.1)fails with
provided group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
Could not find provided
Do you know why ?
Do not hesitate to merge request or commit at https://github.com/moueza/spring-mvc-hello-world-example-mkyong-gradle/issues/2

Because there is no such providedconfiguration. There is providedCompile and providedRuntime. Read the documentation.

Related

Liferay 7 Including third party library package (JsonPath) into portlet module

I'm trying to include JsonPath Library into my Liferay MVC Portlet.
I found thread on Liferay Help Center:
https://help.liferay.com/hc/en-us/articles/360028710272-Resolving-Third-Party-Library-Package-Dependencies
but still I don't know what to do exactly.
I read that i should use compileInclude in build.gradle file, because it's include also dependences for library I want to.
That's how it's look like
#build.gradle
dependencies {
compileOnly group: "com.liferay.portal", name: "release.portal.api"
cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
compileInclude group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
}
When I build a *.jar which i deploy to Liferay I have that error.
2021-07-15 06:56:16.994 INFO [com.liferay.portal.kernel.deploy.auto.AutoDeployScanner][AutoDeployDir:272] Processing mycustomportlet2-1.0.0.jar
2021-07-15 06:56:26.140 ERROR [fileinstall-directory-watcher][DirectoryWatcher:1159] Unable to start bundle: file:/opt/liferay/osgi/modules/mycustomportlet2-1.0.0.jar
com.liferay.portal.kernel.log.LogSanitizerException: org.osgi.framework.BundleException: Could not resolve module: mycustomportlet2 [1411]_ Unresolved requirement: Import-Package: com.google.gson_ [Sanitized]
at org.eclipse.osgi.container.Module.start(Module.java:444) ~[org.eclipse.osgi.jar:?]
at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:428) ~[org.eclipse.osgi.jar:?]
at com.liferay.portal.file.install.internal.DirectoryWatcher._startBundle(DirectoryWatcher.java:1142) [bundleFile:?]
at com.liferay.portal.file.install.internal.DirectoryWatcher._startBundles(DirectoryWatcher.java:1175) [bundleFile:?]
at com.liferay.portal.file.install.internal.DirectoryWatcher._startAllBundles(DirectoryWatcher.java:1120) [bundleFile:?]
at com.liferay.portal.file.install.internal.DirectoryWatcher._process(DirectoryWatcher.java:1032) [bundleFile:?]
at com.liferay.portal.file.install.internal.DirectoryWatcher.run(DirectoryWatcher.java:272) [bundleFile:?]
I tried including also gson in build.gradle but then error shows another library that have unresolved requiremnt.
I'm using Liferay Portal 7.4-ga2 docker image with Java 11.
If you know how to do this without including a lot dependecies manually I will really appreciate a solution for that.
EDIT
I created build.gradle file with all includes needed to work. That's how it look:
dependencies {
compileOnly group: "com.liferay.portal", name: "release.portal.api"
cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
compileInclude group: 'com.jayway.jsonpath', name: 'json-path', version: '2.6.0'
compileInclude group: 'com.google.code.gson', name: 'gson', version: '2.8.7'
compileInclude group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.12.3'
compileInclude group: 'org.apache.tapestry', name: 'tapestry-json', version: '5.7.2'
compileInclude group: 'org.apache.tapestry', name: 'commons', version: '5.7.2'
compileInclude group: 'org.codehaus.jettison', name: 'jettison', version: '1.4.1'
compileInclude group: 'org.json', name: 'json', version: '20210307'
compileInclude group: 'org.slf4j.impl', name: 'log4j12', version: '1.7.2'
}
repositories {
maven {
url "https://maven.averbis.com/m2/"
}
}
If you compileInclude external resources (which is possible, but should be your last resort), unfortunately you will need to include all transitive dependencies as well. You're including jayway/jsonpath, and gson is missing. So you'll need to compileInclude gson. And as you say, when you do that, a different library is missing - so you'll need to include it as well.
That's part of the reason why this should be your last resort.
An alternative is: Check if jayway/jsonpath or gson are OSGi bundles themselves - in which case you can just drop them into Liferay's deploy folder and they'll be dynamically resolved. Of course, in this case their transitive dependencies need to be resolvable as well, so you might need to deploy a couple more bundles than just these two. But this way, all modules that use these libraries will share the same bundle.
Either way, you can inspect a bundle's MANIFEST.mf for imports to figure out what they depend on. Note: there are mandatory and optional dependencies in there. You'll need to satisfy the mandatory ones and the optional ones that you're using. If the libraries in question aren't bundles, they're managing their dependencies differently. I'd at least suggest to the project teams to OSGi'ify their packages - but that's a fix for the long run.
There's a chapter on this on Liferay's University's (free, registration required) course OSGi Basics, called "Bringing along your dependencies" (disclaimer: by yours truly), where I still like the animated special effect visualizing the option to compileInclude and what it does to file size)

Compile dependencies dynamically based on spring.profiles.active

Can someone please guide me where to start this task?
I'd simply have to exclude spring-boot-starter-tomcat when deploying to jboss.
I imagine it will look something like:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web"){
if(getProperty "spring.profiles.active" == "qat")
exclude module: "spring-boot-starter-tomcat"
}
testCompile('org.springframework.boot:spring-boot-starter-test')
}
With the sample above, I get an error:
Could not get unknown property 'spring.profiles.active' for DefaultExternalModuleDependency{group='org.springframework.boot', name='spring-boot-starter-web', version='null', configuration='default'} of type org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency.
Maybe I could create a custom task to set spring.profiles.active on the task. HELP!
As Peter Ledbrook mentioned, gradle does not have access to spring-boot's application.yml at compile time. And the dependencies run very early in gradle's lifecycle that a task is never called before dependencies are resolved.
Even trying dependency resolution strategy was futile.
So I just had to do:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
if(System.getProperty("spring.profiles.active") == "qat"){
exclude module: "spring-boot-starter-tomcat"
}
}
compile("org.springframework.boot:spring-boot-starter-security")
if(System.getProperty("spring.profiles.active") == "qat"){
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Then I'll type gradle build -Dspring-profiles-active=qat when deploying to jboss. and gradle bootRun -Dspring-profiles-active=dev when I have to run locally.

SpringBoot test module doesn't search log4j2 configuration in the right classpath

I'm having some problems running tests in my SpringBoot project.
The project-structure is the following:
Project Structure Image
I can start the resourceService without issues, but if i even try to run the standard test of SpringBoot projects.....
package com.pcsystem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class ResourceserviceApplicationTests {
#Test
public void contextLoads() {
}
}
the program respond with this error:
Logging system failed to initialize using configuration from 'resourceservice/log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\resourceservice\log4j2.xml (Unable to find the specified classpath)
So i tried to change the application.properties specific property from
logging.config=resourceservice/log4j2.xml
to
logging.config=log4j2.xml
After the change i've noticed that resourceserviceApplication will not start because it can't find the log4j2.xml:
Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml (Unable to find the specified classpath)
I've tried to resolve in many ways and doing a lot of researches but at the moment i'm still stuck here.
Any idea?
ps: it seems that the Authorizationservice module doesn't suffer the same problem, but simply because i haven't set the logging.config property in Authorizationservice's application.properties (there is no need for now)
Thanks in advance and have a great day.
-UPDATE 1-
Configuration file is about all the resourceService module, so i've done as you said Kostiantyn ( thanks for you response ) but the problem still persist.
Actual situation:
Project structure after your reply
Now resourceServiceApplication won't start , saying :
Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml
and the contextLoads() method from the test package says:
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\log4j2.xml
Let me show you configuration file:
server.port=8888
logging.config=log4j2.xml
spring.data.mongodb.host=localhost
spring.data.mongodb.database=jogging
#spring.data.mongodb.username=admin
#spring.data.mongodb.password=pass
spring.data.mongodb.port=27017
As requested from user1615664 , below you can see my gradle file
(that's the gradle file about the resourceService module;
AuthorizationService have one specific gradle file and lastly there is
a root gradle file, that i will show to you at the end of this update)
Exscuse me for the lenght, i'm using a large number of libraries here.
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
compile.exclude group:'ch.qos.logback'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude module: "logback-classic"
}
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile group: 'org.apache.tika', name: 'tika', version: '1.16', ext: 'pom'
compile group: 'org.apache.tika', name: 'tika-parsers', version: '1.16'
compileOnly('org.projectlombok:lombok')
compile("org.springframework.boot:spring-boot-starter-actuator")
//compile project(':authorizationservice')
// https://mvnrepository.com/artifact/org.springframework.hateoas/spring-hateoas
compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version: '0.23.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.1.1.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.8.RELEASE'
// https://mvnrepository.com/artifact/org.apache.axis/axis
compile group: 'org.apache.axis', name: 'axis', version: '1.4'
// https://mvnrepository.com/artifact/axis/axis-jaxrpc
compile group: 'axis', name: 'axis-jaxrpc', version: '1.4'
// https://mvnrepository.com/artifact/commons-discovery/commons-discovery
compile group: 'commons-discovery', name: 'commons-discovery', version: '0.5'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
// https://mvnrepository.com/artifact/javax.xml/jaxrpc-api
compile group: 'javax.xml', name: 'jaxrpc-api', version: '1.1.1'
// https://mvnrepository.com/artifact/org.apache.xmlrpc/xmlrpc
compile group: 'org.apache.xmlrpc', name: 'xmlrpc', version: '3.1.3', ext: 'pom'
// https://mvnrepository.com/artifact/javax.activation/activation
compile group: 'javax.activation', name: 'activation', version: '1.1.1'
// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
// https://mvnrepository.com/artifact/wsdl4j/wsdl4j
compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.3'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
PS: maybe is worthless , but in the root module ( baseProjectOauth2 )
we can admire this root gradle file
group 'com.pcsystem'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
You've missing src/test/resources folder in your project structure, and looks like current location of log4j2.xml in the project root is not under classpath at all
Rely on Gradle project convention and place your config files accordingly, i.e. create either src/test/resources and put file in there, if your logging configuration is test specific:
src
test
java
...
resources
log4j2.xml
Or, if logging configuration inside your log4j2.xml will be used by your application during live run (included into .jar deliverable), move this file to src/main/resources instead
that will bring your logging.config=log4j2.xml working.
Further reading about Gradle project structure & resources folders is here
I feel greedy replying to my own question, but i think i've solved.
I've done these things:
-> created resource folder under test module
src
main
test
java
resources
application.properties
In that properties file i've only put this setup
logging.config=log4j2.xml
And all i have to say now is
"explicative image here"
Simply i have to deep study spring structure (and even gradle one) about auto detect of properties files
You can try to put value classpath:log4j2.xml instead of just log4j2.xml into your application.properties (or application.y[a]ml) file.
All the resources, including this file, should be placed inside the src/main/resources (where application.properties file is also placed). Regardless of a few possible exceptions.
If you want a different configuration of logging for tests, than you should put adapted file inside of tests' resources directory (src/test/resources), but if you want these to be the same, then you can leave it as is (inside src/man/resources only)

Gradle IntelliJ Idea

when i add slf4j-api-1.6.4.jar and slf4j-nop-1.6.4.jar in my libs folder it works fine but if i replaced them with gradle dependency then while executing the jar it gives me below error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
these are gradle dependencies for my project for above libraries.
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.4'
compile group: 'org.slf4j', name: 'slf4j-nop', version: '1.6.4'
Make sure you've run Gradle Build, it may not have pulled your dependencies.
Make sure that you include these dependencies in the jar file (like this).

Gradlew doesn't add dependencies to my external libraries(class path) in IDEA 16.2

tl;dr; adding adding dependencies to build.gradle downloads it fine but doesn't add it to the classpath/external libraries in idea.
Hi guys
Im new to developing webapps in java, and im trying to depend on a few jars on mvnrepository.com, the only time the dependencies are downloaded into the external libraries and added to the classpath is when i import the project as a gradle project, as in, each time i have a project up and running and i add a new dependency i would have to import the whole project into intellij again.
my build.gradle file looks like this:
group 'project_name'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/com.google.inject/guice
compile group: 'com.google.inject', name: 'guice', version: '3.0'
// https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.0.M9'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-core
compile group: 'com.sun.jersey', name: 'jersey-core', version: '1.19.1'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-json
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.1'
// https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.23.2'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet
compile group: 'com.sun.jersey', name: 'jersey-servlet', version: '1.19.1'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-server
compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.19.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.5'
}
When i add a new dependency to the list, and run ./gradlew build, with or without the --refresh-dependencies option it does download the new dependencies but it doesn't add the downloaded files to the external libraries/classpath so i can't import them into the java code. I saw a question similar to this one, where they accepted answers like running:
./gradlew idea
In my case this doesn't help at all, it just adds some autogenerated files in the directory with no clear difference to behavior.
Then they accepted importing the project as a gradle project aswell, which i have done - which works, but adding new dependencies doesn't work.
FYI I am using the gradle 2.5 wrapper and IDEA community 16.2
Okay. I solved/figured it out, Apparently it didn't help to just run build,
inside of intellij i had to go to View --> Tool Windows --> Gradle, it then opens the gradle window, where i could click the refresh button, which downloads the dependencies.
Thanks to anyone who looked it over :)

Categories

Resources