简体   繁体   English

来自src / test的Jar文件中的主类

[英]Main class in Jar file from src/test

I need to distribute one of our integration tests, it lives under src/test. 我需要分发一个集成测试,它存在于src / test下。 So I'm using gradle to build a jar pointing to this class. 所以我使用gradle来构建一个指向这个类的jar。

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. 主要方法是那里,'jar tf'确实向我展示了包中的类。

What am I missing here? 我在这里错过了什么?

Your jar file is missing test-runtime dependencies. 您的jar文件缺少测试运行时依赖项。 One possible solution is to package all required dependencies into your jar as well. 一种可能的解决方案是将所有必需的依赖项打包到jar中。 Note that this will increase the size of your distributable jar. 请注意,这将增加可分发jar的大小。

Add an additional from clause to your jar task: 在jar任务中添加一个额外的from子句:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 src / main / resources中的Maven Jar主类 - Maven Jar main class in src/main/resources 无法从src \\ test文件夹“运行文件” - >找不到主要类或无法加载主类 - Cannot “Run File” from src\test folder -> main class not found or could not be loaded 如何在JUnit测试中从src / main / resources中读取文件? - How to read file from src/main/resources relative in a JUnit test? 如何从 Java 的主类访问 src.test 包或如何从主类运行测试类? - How to make acces to src.test package from main class in Java or how to run test class from main class? want to execute a java test class which is in src/test/java from src/main/java but I am unable to import that java class in src/main/java? - want to execute a java test class which is in src/test/java from src/main/java but I am unable to import that java class in src/main/java? 是否可以从 src/main 中的 src/test 加载测试类? - Is it possible to load test classes from src/test in src/main? jar文件找不到主类 - jar file not finding main class Jar文件-找不到主类 - Jar File - main class not found 从jar文件中,从类中调用main方法,该类从jar外部实现一个类 - From a jar file, calling the main method from a class that implements a class from outside the jar 从 Spring 引导 jar 文件运行非主 class - Run a non main class from Spring Boot jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM