动态添加jar到运行java项目(主项目)

时间:2017-05-03 08:37:08

标签: java gradle axis2 executable-jar gradle-eclipse

我需要将运行应用程序生成的jar添加到相同的应用程序类路径中。

Flow - 我已经使用java中的轴2从WSDL生成了一组类文件(打包为jar)。现在我需要访问同一java项目中生成的jar中的.class文件。

我使用了Build Tool Gradle 这是我的代码,为生成的java文件集构建jar。

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

为当前java应用程序构建jar的另一个Gradle文件(具有main函数) 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

这里customProp是变量,用于在运行gradle构建时动态获取jar名称。(gradle build -P customProp = XXXXtask)。

BottomLine:我无法从动态生成的jar访问类。所以问题变成了 - 如何在运行时将jar文件添加到正在运行的应用程序中?。提前感谢。

0 个答案:

没有答案