简体   繁体   中英

Main class in Jar file from src/test

I need to distribute one of our integration tests, it lives under src/test. So I'm using gradle to build a jar pointing to this class.

I figured this out from poking around the net:

task fatJar(type: Jar) {
  zip64 = true
  manifest {
    attributes 'Implementation-Title': 'Gradle Jar File Example',
            'Implementation-Version': 1.3,
            'Main-Class': 'org.example.AnIntegrationTest'
  }
  from sourceSets.test.output
}

When I try running this I get the:

Error: Could not find or load main class org.example.AnIntegrationTest

The main method is there, and 'jar tf' does show me the class in the package.

What am I missing here?

Your jar file is missing test-runtime dependencies. One possible solution is to package all required dependencies into your jar as well. Note that this will increase the size of your distributable jar.

Add an additional from clause to your jar task:

task fatJar(type: Jar) {
  zip64 = true
  manifest {
    attributes 'Implementation-Title': 'Gradle Jar File Example',
            'Implementation-Version': 1.3,
            'Main-Class': 'org.example.AnIntegrationTest'
  }
  from sourceSets.test.output

  //collect all dependencies
  from { configurations.testRuntime.collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM