How download jar file with pom file in gradle folder? - eclipse

I add compile 'org.apache.commons:commons-vfs2-project:2.3' to my build.gradle file, but I found there is ~\.gradle\caches\modules-2\files-2.1\org.apache.commons\commons-vfs2-project\2.3\860b837e62ab6d3282e7ff96f63ea21aebcdba40\commons-vfs2-project-2.3.pom, how can I download jar file?

Here is one of the solution.
Below is the build.gradle for a sample project
I have included 2 plugins and written script to include additional scripts while building jar.
There are some eclipse generated scripts which I have not removed.
apply plugin: 'java'
/**Following plugins are needed */
apply plugin: 'maven-publish'
apply plugin: 'maven'
/**Just to include jar versioning*/
version = '1.0.0'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.25'
testCompile 'junit:junit:4.12'
}
apply plugin: 'eclipse'
eclipse {
classpath {
downloadSources = true // default: true
downloadJavadoc = true // default: false
}
}
def eclipseSourceFolders=[
'src/main/java',
'src/main/resources',
'src/test/java',
'src/test/resources'
];
tasks.eclipse.dependsOn << {
for (String sourceFolder: eclipseSourceFolders){
def resourceDir = new File(project.projectDir, sourceFolder)
if( !resourceDir.exists() && ! resourceDir.mkdirs() ) {
logger.info("Not able to create %1",resourceDir);
}
}
}
/**
Add Following scripts to placs pom.xml within jar and .pom file within libs folder*/
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
/** While creating jar generate and place pom.xml also place .pom file in libs folder */
jar {
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForMavenJavaPublication }
rename ".*", "pom.xml"
}
doLast {
pom {
project {
}
}.writeTo("$buildDir/libs/$project.name-${version}.pom")
}
}

Related

Eclipse & Grade how to generate jar so that debugger can access the source code?

I am using Eclipse 4.20.0 with Gradle plugin. (Gradle version 6.6)
I have created a jar.
I have created another project that references this jar.
I want to debug the last project, but when a class from the jar is invoked, Eclipse displays the file with extension ".class", but cannot find the source file with extension ".java" and displays "Source not found" "The JAR of this class file belongs to container 'Project and External Dependencies' which does not allow modifications to source attachments on its entries"
the build.gradle file for the project that creates the jar is:
plugins {
id 'java-library'
}
repositories {
jcenter()
mavenCentral()
}
java {
withSourcesJar() //For including the sources??
}
// My customization
project.jar.destinationDirectory = file("$rootDir/../mytargets") //Save jars in the same folder
project.archivesBaseName = 'a-annotations' //Set the name of the jar file
project.version = '1.0'
dependencies {
testImplementation 'junit:junit:4.13'
}
The build.gradle for the project that uses this jar is
plugins {
id 'java-library'
id 'java-library-distribution'
}
repositories {
jcenter()
mavenCentral()
}
java {
withSourcesJar() //For including the sources??
}
// My customization
project.jar.destinationDirectory = file("$rootDir/../mytargets") //Save jars in the same folder
project.archivesBaseName = 'p-if07-run'
project.version = '1.0'
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13'
api files('../mytargets/a-annotations-1.0.jar') // The jar !!!!
}
// Collect all the dependencies into the folder "lib"
jar {
manifest {
attributes(
'Main-Class': 'Execute',
'Class-Path': configurations.runtimeClasspath.files.collect { 'lib/'+it.getName() }.join(' ')
)
}
}
Finally, replacing the jar part of the build.gradle file with this one, solved my problem
jar {
from sourceSets.main.allSource //Include java sources
}

Idea cannot recognize a symbol even it is present in external dependencies

Does somebody understands what is a problem? Why my IDE doesn't see classes from dependencies? Idea version: 17.2.2
The root build.gradle:
subprojects.each {
apply plugin: 'idea'
}
The root setting.gradle:
include 'client'
include 'api'
rootProject.name = 'app-1-akka-reactjs'
My build.gradle of api project:
apply plugin: 'play'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.iatoki:gradle-play-idea:0.4.1'
}
}
apply plugin: 'org.iatoki.play-idea'
repositories {
jcenter()
ivy {
url "https://repo.typesafe.com/typesafe/ivy-releases/"
layout "pattern", {
ivy "[organisation]/[module]/[revision]/ivys/ivy.xml"
artifact "[organisation]/[module]/[revision]/jars/[artifact].[ext]"
}
}
}
model {
components {
play {
platform play: '2.5.8', scala: '2.11', java: '1.8'
injectedRoutesGenerator = true
}
}
}
dependencies {
play 'com.typesafe.play:play-slick_2.11:2.1.0'
play 'com.typesafe.play:play-slick-evolutions_2.11:2.1.0'
play 'org.postgresql:postgresql:9.4-1200-jdbc41'
}
The build.gradle of client is empty for now.
To generate idea's files I've used:
gradle cleanIdea idea
I faced a similar issue. I could resolve it by moving/copying the settings.xml file from $MVN_HOME/conf to ~/.m2 folder. As MVN_HOME was not detected by Idea it could not resolve the settings.xml from MVN_HOME.

Unable to import spring-boot gradle project in eclipse workspace

Gradle setup for Spring-boot application : build.gradle file i have used sourcesSet where my all java source file resides.
buildscript {
repositories {
//Required repos
mavenCentral()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
mavenLocal()
}
dependencies {
//Required dependency for spring-boot plugin
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.1.2.BUILD-SNAPSHOT'
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'spring-boot'
apply plugin: 'eclipse-wtp'
bootRepackage {
mainClass = 'com.test.app.Application'
}
war {
baseName = 'companies'
version = '0.1'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
mavenLocal()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
//Required dependency for JSP
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
}
sourceSets {
main {
java {
srcDir 'app/GeneratedSource'
srcDir 'app/JavaSource'
srcDir 'web/JavaSource'
}
resources {
srcDir 'app/GeneratedSource'
srcDir 'app/JavaSource'
srcDir 'web/JavaSource'
}
}
}
eclipse {
classpath {
defaultOutputDir = file('web/WebContent/WEB-INF/classes')
}
}
webAppDirName = 'web/WebContent'
libsDirName = 'war'
war {
baseName = baseName
version = appVersion
archiveName = warName
}
task printClasspath {
doLast {
configurations.testRuntime.each { println it }
}
enter image description hereCommand Line able to build model
When in eclipse while importing gradle project it is showing error
Iam unable to import Gradle project in Eclipse
Do you have a Gradle plugin for eclipse?
Help -> Eclipse MArketplace -> Search "Gradle" -> Install the Gradle STS integration.
In addition I recomend you change Spring Boot version to '1.3.3.RELEASE'.
buildscript {
ext {
springBootVersion = '1.3.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}}
In my case there was an update available for the gradle plugin. Once I updated the plugin using eclipse marketplace everything worked fine and project was imported properly.

Is it possible to extend a gradle build script configured in a binary plugin?

I've created a Gradle plugin below:
class CommandServiceProjectPlugin implements Plugin<Project> {
public void apply(Project project) {
project.buildscript{
repositories {
maven: {
url: 'http://localhost:8081/artifactory/zailab-virtual-repo'
credentials: {
username = "admin"
password = "password"
}
}
}
/*Spring Boot Gradle plugin */
dependencies {
classpath: 'org.springframework.boot:spring-boot-gradle-plugin:1.1.6.RELEASE'
}
}
project.apply plugin: 'spring-boot'
project.apply plugin: 'java'
project.apply plugin: 'eclipse'
project.repositories {
maven: {
url: 'http://localhost:8081/artifactory/zailab-virtual-repo'
}
}
project.dependencies {
/*Spring Boot dependencies */
compile: 'org.springframework.boot:spring-boot-starter-test'
compile: 'org.springframework.boot:spring-boot-starter-aop'
compile: 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile: 'org.springframework.boot:spring-boot-starter-integration'
compile: 'org.springframework.boot:spring-boot-starter-amqp'
/*Axon dependencies */
compile: 'org.axonframework:axon-core:2.3.1'
compile: 'org.axonframework:axon-mongo:2.3.1'
}
}
}
I then apply the plugin within another project as below, but it seems the buildscript definitions override/conflict as the 'spring-boot' plugin cannot be found. Am I attempting the impossible or is there perhaps another way to achieve what I am trying to do?
buildscript {
repositories {
maven {
url 'http://localhost:8081/artifactory/zailab-virtual-repo'
credentials {
username = "admin"
password = "password"
}
}
}
dependencies {
classpath(group: 'com.zailab', name: 'zailab-command-service-build', version: '1.0.0- SNAPSHOT')
}
}
apply plugin: 'com.zailab.command.service.project'
Thanks,
Roscoe
As far as I know, it's not possible to add build script dependencies programmatically from a plugin.
Reason for this is build script life cycle - invocation of plugins' apply method happens after the project's classpath configuration had already been resolved.
You should either configure the buildscript in project's build script, or package classpath dependencies with the plugin.

how to copy the dependencies libraries JARs in gradle

I got a runnable jar with this build.gradle
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes("Main-Class" : "com.test.HelloWorld")
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
but it run failed, because the dependencies jars can't find.
and then I add this code:
task copyToLib(type: Copy) {
into "$buildDir/output/libs"
from configurations.runtime
}
but nothing change. I can't find the folder output/libs.
how can I copy the dependencies libs jars to a specified folder or path?
Add:
build.dependsOn(copyToLib)
When gradle build runs, Gradle builds tasks and whatever tasks depend on it (declared by dependsOn). Without setting build.dependsOn(copyToLib), Gradle will not associate the copy task with the build task.
So:
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes('Main-Class': 'com.test.HelloWorld')
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
task copyToLib(type: Copy) {
into "${buildDir}/output/libs"
from configurations.runtime
}
build.dependsOn(copyToLib)
I find the application plugin way too cumbersome and too verbose in its output. Here's how I finally got a setup I was happy with, i.e., create a distribution zip file with dependency jars in subdirectory /lib and add all dependencies to Class-Path entry in the manifest file:
apply plugin: 'java'
apply plugin: 'java-library-distribution'
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.commons:commons-lang3:3.3.2'
}
// Task "distZip" added by plugin "java-library-distribution":
distZip.shouldRunAfter(build)
jar {
// Keep jar clean:
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
manifest {
attributes 'Main-Class': 'com.somepackage.MainClass',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
}
// How-to add class path:
// http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle
// https://gist.github.com/simon04/6865179
}
Hosted as a gist here.
The result can be found in build/distributions and the unzipped contents look like this:
lib/commons-lang3-3.3.2.jar
MyJarFile.jar
Contents of MyJarFile.jar#META-INF/MANIFEST.mf:
Manifest-Version: 1.0
Main-Class: com.somepackage.MainClass
Class-Path: lib/commons-lang3-3.3.2.jar
Since Gradle 6.0 it is:
tasks {
val deps by registering(Copy::class) {
from(configurations.runtimeClasspath)
into("build/deps")
}
}
The problem with all the previous answers is that they only collect dependencies from one configuration. To get ALL of the dependencies, you should use this:
task saveDependencies(type: Copy){
configurations.each {
if (it.isCanBeResolved())
from it into "gradle_dependencies"
}
from buildscript.configurations.classpath into "gradle_dependencies"
}
The application plugin requires you to set the main class name like this:
mainClassName = "com.test.HelloWorld"
You will need to add that to your build script. Keep in mind that if you try to run your application with the java command you will also need to set the classpath with -cp.
The application plugin simplifies this process by providing the task distZip. If you run that task you a full distribution is created for you under build/distributions. The distribution contains start scripts and all dependencies. The generated start scripts already set the classpath for you so you don't have to deal with it anymore.
The java plugin can pack a jar with dependencies and there's no need for the application plugin. A task like the following would do:
task buildWithDeps(type: Jar) {
manifest {
attributes "Main-Class": "com.test.HelloWorld"
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
As of at least Gradle 5.6.4 you'll want to do something closer to this.
dependencies {
implementation 'my.group1:my-module1:0.0.1'
implementation 'my.group2:my-module2:0.0.1'
}
jar {
from {
configurations.compileClasspath.filter { it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
}
}
For Gradle 7.4 with Groovy:
configurations {
externalLib.extendsFrom(implementation)
}
task copyLibs(type: Copy){
from configurations.externalLib{
into '<dest-dir-name>'
exclude('<if any jars need to be excluded>')
}
}