How to import jar file into project with gradle

i would like to use spring framework in my project with gradle

in build.gradle, i put
dependencies {
compile group: ‘org.apache.logging.log4j’, name: ‘log4j-api’, version: '2.7’
compile group: ‘org.apache.logging.log4j’, name: ‘log4j-core’, version: '2.7’
testCompile group: ‘junit’, name: ‘junit’, version: '4.12’
compile ‘org.springframework:spring-context:4.3.3.RELEASE’

it builds successfully.

I know the dependencies is collected into WAR file, but they are not added to the WEB-INF/lib folder.
So there are errors when i import org.springframework.stereotype.Component in the java file.
Do i need to download the dependencies and put them in WEB-INF/lib by myself?
if i have to download files manually to use them? why we use gradle to build the project?

1 Like

That shouldn’t be necessary at all, the war plugin copies any dependencies of the runtime configuration (which extends compile) to WEB-INF/lib.
Did you forget to apply the war plugin or are there multiple sub-projects involved and you’re using compileOnly dependencies?

i do apply war plugin in build.gradle, and there are not multiple sub-projects.

apply plugin: 'java’
apply plugin: 'war’
apply plugin: ‘eclipse-wtp’

i import the project into eclipse by import->existing project into workspace, and there are no any dependencies in the WEB-INF/lib

If i import project by import->war file. Then the dependencies in the WebContent/WEB-INF/lib

So your question is actually about Eclipse’s Gradle integration and not about Gradle itself!
Sorry but I can’t help you then, I don’t know enough about Eclipse anymore because I’ve switched to IntelliJ years ago and never looked back. One reason was the superior Maven and Gradle integration… :slight_smile:

But there seems to be a basic misunderstanding:
Executing “gradle war” will build a WAR file containing any runtime dependency in WEB-INF/lib so you may deploy that WAR file without errors. But you will not see those dependencies in your source code! Try executing “gradle war” and look into the created WAR file in the “builds/libs/” folder.

Now an IDE like Eclipse should recognize those dependency automatically (by analyzing build.gradle, no need to execute gradle war) and add those to your module but not to WebContent/WEB-INF/lib. Again you will not see those dependencies in your source code!
WebContent is for your content (like src/main/java for your classes) only which gets version controlled, etc.

I’m sure there is further documentation for Eclipse, try to search for “eclipse gradle war integration”.

Hi, thank you so much for your help:slight_smile: