Java可执行的JAR文件,其中缺少了一个包及其内容。

huangapple go评论73阅读模式
英文:

Java executable jar file with missing package and its contents

问题

我继承了一个Java Maven项目,其中包含了cucumber,junit和selenium框架,除了JUnit之外,我对其他框架都不熟悉。我注意到它的包名位于src/test/java而不是src/main/java

src/test/java
    com.somewhere.test
        FooRunner.java

有人告诉我,我可以通过在Eclipse IDE中右键单击Run as > JUnit Test来从FooRunner类运行JUnit测试。

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources/com/sowhere/test/features",tags = {"@Test"}, plugin = {
    "pretty",
    "junit:target/results.xml",
    "html:target/cucumber"},
    dryRun = false)
public class FooRunner {
    public static void main(String[] args) {
        
    }
}

我能够成功运行其单元测试。我的问题是,我需要这个Maven项目生成可执行的JAR文件,然后能够从中运行其单元测试:

java -jar Foo.jar

我按照这个帖子 https://stackoverflow.com/questions/36149977/running-cucumber-tests-directly-from-executable-jar 中的步骤更新了pom.xml文件,并添加了public static void main方法,但是当我执行JAR文件时,收到以下错误消息:

Error: Could not find or load main class com.somewhere.test.FooRunner

我将JAR更改为ZIP,然后解压了所有内容。令人惊讶的是,我没有看到com.somewhere.test等包名,但我看到了其他外部库的包名,比如cucumber等。是的,META-INF/MANIFEST.MF与正确的数据一起存在。

更新:

我从这个帖子中找到了答案:https://stackoverflow.com/questions/36047637/how-can-i-include-test-classes-into-maven-jar-and-execute-them

英文:

I inherited java maven project composed of cucumber, junit and selenium frameworks which I am new to except Junit. I noticed its pkg name is under src/test/java instead of src/main/java.

src/test/java
    com.somewhere.test
        FooRunner.java

I was told that I can run junit test from FooRunner class by right click Run as > Junit Test in eclipse IDE.

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources/com/sowhere/test/features",tags = {"@Test"}, plugin = {
    "pretty",
    "junit:target/results.xml",
    "html:target/cucumber"},
    dryRun = false)

public class FooRunner {
  public static void main(String[] args) {
	
  }
}

I was able to run its unit test just fine. My question is I need this mvn project to generate executable jar file then be able to run its unit test from

java -jar Foo.jar

I followed this thread to https://stackoverflow.com/questions/36149977/running-cucumber-tests-directly-from-executable-jar by updating pom.xml file and adding public static void main method however when I executed the jar file I received following error msg..

Error: Could not find or load main class com.somewhere.test.FooRunner

I changed jar to zip then unpacked it all. Surprisingly I didn't see pkg name com.somewhere.test but I see other pkgs of external libraries such as cucumber and etc. Yes META-INF/MANIFEST.MF is there with proper data I believe.

I am curious to know why the pkg (i.e. com.somewhere.test) is missing along with class (i.e. FooRunner.class)

Update:

I found an answer from this thread: https://stackoverflow.com/questions/36047637/how-can-i-include-test-classes-into-maven-jar-and-execute-them

答案1

得分: 1

Maven有一个src/main/java/,其中的类会放入产品jar包中,还有一个src/main/test/java/,其中的类会放入测试jar包中(或仅用于测试),在运行应用程序时不需要。这种分离是有意义的。测试jar包也依赖于生产jar包,因为它测试了那些类。

可以使用目标jar:test来创建这个测试jar包

英文:

Maven has a src/main/java/ whose classes go into the product jar, and src/main/test/java/ whose classes go in a test jar (or are just tested), not needed to run the application. This separation makes sence. The test jar depends on the production jar too, as it tests those classes.

This test jar can be created with goal jar:test

huangapple
  • 本文由 发表于 2020年7月24日 04:30:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63062663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定