简体   繁体   English

如何使用Maven生成JAR

[英]How to generate a JAR using maven

I have a Springboot application that runs a CLI app. 我有一个运行CLI应用程序的Springboot应用程序。

Here's my main class: 这是我的主要课程:

@SpringBootApplication
public class MyApplication {

    @Autowired
    GameClient gameClient;

    @PostConstruct
    public void postConstruct(){
       gameClient.runGame(); 
    }

    public static void main(String[] args) {
        SpringApplication.run(GameApplication.class, args);         
    }
}

When I run the command mvn package to generate a JAR, Spring executes the postConstruct() method and starts my CLI application instead of properly generating my JAR. 当我运行命令mvn package生成JAR时,Spring执行postConstruct()方法并启动CLI应用程序,而不是正确生成JAR。

When I remove the postConstruct() the JAR is successfully generated, but I need this method becaus it is responsible for running my CLI app. 当我删除postConstruct() ,将成功生成JAR,但是我需要此方法,因为它负责运行CLI应用程序。

How can I solve it? 我该如何解决?

I solved it by skiping the tests when generationg the JAR: 我通过生成JAR时跳过测试来解决它:

mvn package -Dmaven.test.skip=true

mvn package was invoking my tests, which internally initialized all the beans and, as part of initialization, Spring invokes @PostConstruct methods. mvn package正在调用我的测试,该测试内部初始化了所有bean,并且作为初始化的一部分,Spring调用了@PostConstruct方法。

The issue is that gameClient.runGame() appears to block your test, either by running infinitely, or by requesting user input. 问题是gameClient.runGame()似乎通过无限运行或请求用户输入来阻止您的测试。 If you have any tests running your Spring boot application, your test (and your build) will block as well. 如果您有任何运行Spring Boot应用程序的测试,那么您的测试(以及您的构建)也会阻塞。

Even though you can pass the -Dmaven.test.skip=true parameter to skip tests (as mentioned in this answer ), it still means that that specific test is broken. 即使您可以传递-Dmaven.test.skip=true参数以跳过测试(如本答案中所述 ),它仍然意味着特定测试已损坏。 Either remove it if you don't need it, or make sure that gameClient.runGame() isn't invoked during a test by doing the following: 如果不需要,请删除它,或者通过执行以下操作来确保在测试过程中未调用gameClient.runGame()

Move your logic to a separate class that implements the CommandLineRunner (or ApplicationRunner ) interface: 将逻辑移到实现CommandLineRunner (或ApplicationRunner )接口的单独的类中:

@Component
public class GameRunner implements CommandLineRunner {
    @Autowired
    GameClient gameClient;

    @Override
    public void run(String...args) throws Exception {
        gameClient.runGame();
    }
}

After that, annotate the component with the @Profile annotation, for example: 之后,使用@Profile注释对组件进行注释,例如:

@Component
@Profile("!test") // Add this
public class GameRunner implements CommandLineRunner {
    @Autowired
    GameClient gameClient;

    @Override
    public void run(String...args) throws Exception {
        gameClient.runGame();
    }
}

By using the @Profile annotation, you can tell Spring to only load the component when certain profiles are active. 通过使用@Profile批注,您可以告诉Spring仅在某些配置文件处于活动状态时才加载组件。 By using the exclamination mark ! 通过使用感叹号! , we tell Spring to only load the component if the test profile is not active. ,我们告诉Spring仅在测试配置文件未激活时才加载组件。

Now, within your test, you can add the @ActiveProfiles("test") annotation. 现在,在测试中,您可以添加@ActiveProfiles("test")批注。 This will enable the test profile when running the test, and that means that the GameRunner bean will not be created. 这将在运行测试时启用测试配置文件,这意味着将不会创建GameRunner bean。

First you need to update in pom file. 首先,您需要在pom文件中进行更新。

<modelVersion>4.0.0</modelVersion>
    <groupId>com.example.eg</groupId>
    <artifactId>eg</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

then run this command (mvn package -Dmaven.test.skip=true) 然后运行此命令(mvn软件包-Dmaven.test.skip = true)

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

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