I need to add jar generated from running application into same application classpath.
Flow - i have generated set of class files(packaged as jar) from WSDL using axis 2 from java .Now i need to access the .class file in generated jar on same java project.
I have used Build Tool Gradle
Here is my code , to build jar for generated set of java files.
buildscript {
ext {
springBootVersion = '1.5.2.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()
}
dependencies {
sourceCompatibility = 1.8
bootRepackage {
enabled = false
}
sourceSets {
compile fileTree(dir:'C://Users//10Decoders//Downloads//axis2-1.7.4-bin//axis2-1.7.4//lib',include: '*.jar')
preBuildSources
main {
java {
srcDirs = ['../'+customProp]
println "sourceSets -- > DONE ---"+customProp
}
}
}
task copypackage(type: Copy){
println "COpy Jar to package - > "
into customProp
from customProp+'/src/'
}
jar {
archiveName = customProp+'.jar'
destinationDir = projectDir
println "JAR BUILD---- > "
}
}
task mydatafile(dependsOn:build){
println "COpy Jar to claspath - > "+customProp
doLast{
copy{
into "../"
from customProp+'.jar'
}
}
}
build.dependsOn copypackage
Another Gradle file to build jar for current java application(Which has main function)
Build.script 2
buildscript {
ext {
springBootVersion = '1.5.2.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
dependencies {
compile 'org.springframework:spring-support:2.0.8'
compile 'org.springframework:spring-aop:4.1.2.RELEASE'
compile 'org.springframework:spring-beans:4.1.2.RELEASE'
compile 'org.springframework:spring-context:4.1.2.RELEASE'
compile 'org.springframework:spring-core:4.1.2.RELEASE'
compile 'com.squareup.okio:okio:1.11.0'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'org.json:json:20160810'
compile 'mysql:mysql-connector-java:5.1.38'
compile 'org.apache.commons:commons-dbcp2:2.0.1'
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.1.Final'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile group: 'com.predic8', name: 'soa-model-core', version: '1.5.4'
// https://mvnrepository.com/artifact/wsdl4j/wsdl4j
compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'
// https://mvnrepository.com/artifact/org.reflections/reflections
compile group: 'org.reflections', name: 'reflections', version: '0.9.5-RC2'
}
jar {
archiveName = 'batch.jar'
destinationDir = projectDir
manifest {
attributes (
'Main-Class': 'com.wsdl.migration.SpringWsdlFieldIdentification',
'Class-Path': 'testAPP.jar mydata/'+customProp+'.jar'
)
}
}
task copyLibs(type: Copy){
println "buildDir- > "+buildDir
}
build.dependsOn copyLibs
Here customProp is variable to get the jar name dynamically while run the gradle build.(gradle build -P customProp=XXXXtask ).
BottomLine : I cannot able to access the classes from the dynamically generate jar.So the Question becomes - How can i add the jar file into running application on runtime??.Thanks in advance.
Related
I created spring-boot gradle multi-module project which consisted of 3 modules: controller, service, repository. Main file was situated in Controller-module and named MySpringBootApplication.
I could build this project (using gradle build) and could get jar-file. But after starting this jar in command line I took the next error:
Error: Could not find or load main class com.epam.esm.config.MySpringBootApplication Caused by: java.lang.ClassNotFoundException: com.epam.esm.config.MySpringBootApplication.
To fix this bug I added Main-Class attributte to MANIFEST.MF file in main build.gradle but this action didn't help. So could anybody help?
MAIN BUILD.GRADLE FILE
plugins {
id 'java'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.4.3'
id 'application'
}
group = 'com.myproject'
version = 'snapshot-1.0.0'
repositories {
mavenCentral()
}
bootJar {
enabled = false
}
jar {
enabled = true
manifest {
attributes(
"Main-Class": "com.myproject.config.MySpringBootApplication")
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.4.3', ext: 'pom'
}
test {
useJUnitPlatform()
}
subprojects {
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
group = 'com.epam.esm'
version = '1.0.0'
repositories {
mavenCentral()
}
bootJar {
enabled = false
}
jar {
enabled = true
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'org.hibernate', name: 'hibernate-envers', version: '5.4.27.Final'
implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
implementation group: 'org.openidentityplatform.commons', name: 'json-web-token', version: '2.0.11'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.18'
annotationProcessor 'org.projectlombok:lombok'
implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.4.3', ext: 'pom'
}
test {
useJUnitPlatform()
}
}
SETTINGS.GRADLE
rootProject.name = 'module'
include('repository', 'service', 'controller')
The Spring Boot application executable jar file is built by bootJar task, so adding the main-class information via jar won't work either.
The bootJar task tries to create an executable jar, and that
requires a main() method. As a result, you need to disable the
bootJar task and enable the jar task (which creates an ordinary jar
rather than an executable jar) only for your no executable jar
modules.
Since you did it under subprojects section, the controller module will produce a standard jar as well. You may produce standard jars for all modules but excluding the controller module as follows:
subprojects {
if (it.name != 'controller') {
bootJar {
enabled = false
}
jar {
enabled = true
}
}
}
In addition you have to remove the jar section below
jar {
enabled = true
manifest {
attributes(
"Main-Class": "com.myproject.config.MySpringBootApplication")
}
}
and replace
bootJar {
enabled = false
}
with
bootJar {
mainClassName = 'com.myproject.config.MySpringBootApplication'
}
Reference
Creating a Multi Module Project
Could not find method application() for arguments
i tried gradle build but got this error
then i want to build the OneDrive client
i do not want to GET making a returnurl or get it by other means.
plugins {
id 'application'
}
application {
mainClassName = 'com.mycompany.app.App'
}
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src'
}
}
dependencies {
compile 'org.apache.httpcomponents:httpclient:4.5.10'
compile 'org.apache.httpcomponents:httpcore:4.4.12'
compile group: 'org.apache.httpcomponents', name: 'httpclient',
version: '4.5.10'
compile group: 'org.apache.httpcomponents', name: 'httpcore',
version: '4.4.12'
}
repositories {
jcenter()
maven {
url "https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient"
}
maven {
url
I tried to generate the exe using the gradle plugin gradle-launch4j with the following configuration of the build.gradle:
plugins {
id 'java'
id 'edu.sc.seis.launch4j' version '2.4.6'
id 'application'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
// lib via maven
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
compile group: 'org.controlsfx', name: 'controlsfx', version: '8.40.15'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.28.0'
}
jar {
manifest {
attributes 'Main-Class': 'Main'
}
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
application {
mainClassName = 'Main'
}
launch4j {
mainClassName = 'Main'
outfile = 'Gymnasium.exe'
icon = "${projectDir}/src/main/resources/view/img/icona.ico"
jreMinVersion = "1.8.0"
}
Exe is generated but trying to run, it not starts. I noticed that it starts if I launch it from terminal using java -jar Gymnasium.exe command.
Is there something of wrong in my configuration? I tried to search if someone had a similiar issue but I didn't find anything.
Note: If I try to generate the exe from Launch4j application it works perfectly.
I am new to Gradle. With Gradle 3.5, I am trying to create a war build from a java project. Below is my gradle file content.
apply plugin: 'war'
buildDir = "${rootProject.ext.buildGradle}/${project.name}"
def buildClassesDir = buildDir.getAbsolutePath() + '/classes/main'
configurations {
localLibraries
}
task copyNonJava(type: Copy, dependsOn: compileJava) {
from ('src/main/java') {
exclude '**/*.java'
include '**/*.properties'
}
from ('resources') {
include 'default_content.properties'
}
into buildClassesDir
includeEmptyDirs = false
}
task bundleJar (type: Jar, dependsOn: ['compileJava', 'copyNonJava']) {
baseName archivesBaseName
from buildClassesDir
}
task bundleWar (type: War, dependsOn: ['bundleJar']) {
baseName = project.name
from 'web'
webXml = file( 'resources/WEB-INF/web.xml' )
classpath = configurations.localLibraries
}
dependencies {
compile group: 'com.system', name: 'core', version: rootProject.version, changing: true
compile group: 'com.system', name: 'core-ui', version: rootProject.version, changing: true
compile group: 'com.persistence', name: 'persistence', version: '1.0'
compile group: 'com.surveys', name: 'survey', version: '1.0'
localLibraries fileTree("lib") {
exclude 'spring*'
}
}
When I generate war build, it adds jar files under WEB-INF/lib directory. But along with those jar files I want jar files from com.system group and jar file generated from bundleJar task. How can I achieve this?
Libraries from the compile configuration are included in classpath by default but classpath = configurations.localLibraries overrides the default value.
Instead of overriding the default classpath (classpath = ... effectively means setClasspath(...)) you can append additional files to it:
task bundleWar (type: War, dependsOn: ['bundleJar']) {
baseName = project.name
from 'web'
webXml = file( 'resources/WEB-INF/web.xml' )
classpath configurations.localLibraries
classpath bundleJar.archivePath
}
Thank you for the response on this. Really appreciate.
I have figured out a way from this. Learned that gradle follows some directory conventions so make changes to directory structure supported by gradle and made changes to the gradle script.
Here is working gradle script:
apply plugin: 'war'
buildDir = "${rootProject.ext.buildGradle}/${project.name}"
def buildClassesDir = buildDir.getAbsolutePath() + '/classes/main'
configurations {
localLibraries
}
task bundleJar (type: Jar, dependsOn: ['compileJava']) {
baseName archivesBaseName
from buildClassesDir
}
task bundleWar (type: War, dependsOn: ['bundleJar']) {
dependsOn = [ 'bundleJar' ]
baseName = project.name
from 'web'
webXml = file( 'resources/WEB-INF/web.xml' )
}
dependencies {
compile group: 'com.system', name: 'core', version: rootProject.version, changing: true
compile group: 'com.system', name: 'core-ui', version: rootProject.version, changing: true
compile group: 'com.persistence', name: 'persistence', version: '1.0'
compile group: 'com.surveys', name: 'survey', version: '1.0'
// Replace this and make sure all necessary jar dependencies are been fetched from repository instead from file system
/*localLibraries fileTree("lib") {
exclude 'spring*'
}*/
}
I'd like to create a exe file, without having to put all required libraries beside the exe.
Formerly with ant I created a self-contained jar file with one-jar and then wrapped this into a exe file with launch4j.
Gradle has plugins for both and standalone both work very well with almost no configuration.
But how can I manage to use the created one-jar as the input for launch4j?
This is my current buildfile:
apply plugin: 'java'
apply plugin: 'launch4j'
apply plugin: 'gradle-one-jar'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'edu.sc.seis.gradle:launch4j:1.0.6'
classpath 'com.github.rholder:gradle-one-jar:1.0.4'
}
}
launch4j {
mainClassName = "de.my.umkopierer.Umkopierer"
launch4jCmd = "C:/Program Files (x86)/Launch4j/launch4j"
jar = "lib/Umkopierer-1.0.jar"
headerType = "console"
dontWrapJar = false
}
sourceCompatibility = 1.7
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Umkopierer', 'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile 'com.google.guava:guava:18.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.4'
compile 'com.fasterxml.jackson.datatype:jackson-datatype-jdk7:2.4.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
task oneJar(type: OneJar) {
mainClass = "de.stiffi.umkopierer.Umkopierer"
}
I solved this by making the launch4j task 'createExe' dependent on the onejar/fatjar (or any other fat jar creation method). E.g.:
tasks.createExe.dependsOn('oneJar')
task launch4j(overwrite: true, dependsOn: ['createExe']){
}
Also I think that your gradle build file should contain a main class attribute like
manifest {
attributes 'Main-Class':'com.example.MyMainClass'
}
(at least that's the case if you are using the fatjar gradle plugin).