代码之家  ›  专栏  ›  技术社区  ›  andreybleme

如何使用maven生成JAR

  •  1
  • andreybleme  · 技术社区  · 7 年前

    我有一个运行CLI应用程序的Springboot应用程序。

    @SpringBootApplication
    public class MyApplication {
    
        @Autowired
        GameClient gameClient;
    
        @PostConstruct
        public void postConstruct(){
           gameClient.runGame(); 
        }
    
        public static void main(String[] args) {
            SpringApplication.run(GameApplication.class, args);         
        }
    }
    

    当我运行命令时 mvn package postConstruct() 方法并启动CLI应用程序,而不是正确生成JAR。

    当我移除 施工后()

    我该怎么解决?

    3 回复  |  直到 7 年前
        1
  •  2
  •   Dimitri Mestdagh Lancer.Yan    7 年前

    问题是 gameClient.runGame()

    即使你能通过考试 -Dmaven.test.skip=true 跳过测试的参数(如中所述 this answer 游戏客户端.runGame()

    将逻辑移到实现 CommandLineRunner (或 ApplicationRunner )接口:

    @Component
    public class GameRunner implements CommandLineRunner {
        @Autowired
        GameClient gameClient;
    
        @Override
        public void run(String...args) throws Exception {
            gameClient.runGame();
        }
    }
    

    之后,用 @Profile 注释,例如:

    @Component
    @Profile("!test") // Add this
    public class GameRunner implements CommandLineRunner {
        @Autowired
        GameClient gameClient;
    
        @Override
        public void run(String...args) throws Exception {
            gameClient.runGame();
        }
    }
    

    通过使用 @简介 !

    现在,在测试中,可以添加 @ActiveProfiles("test") 注释。这将在运行测试时启用测试配置文件,这意味着 GameRunner

        2
  •  2
  •   andreybleme    7 年前

    我在生成JAR时跳过了测试:

    mvn package -Dmaven.test.skip=true
    

    mvn package @PostConstruct 方法。

        3
  •  0
  •   Tayyab Shahbaz    7 年前

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

    然后运行这个命令(mvn包-Dmaven.test.skip=真)