Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在IDE中以jar的形式运行应用程序,而将maven打包为war?_Java_Spring_Maven_Tomcat_Spring Boot - Fatal编程技术网

Java 如何在IDE中以jar的形式运行应用程序,而将maven打包为war?

Java 如何在IDE中以jar的形式运行应用程序,而将maven打包为war?,java,spring,maven,tomcat,spring-boot,Java,Spring,Maven,Tomcat,Spring Boot,我有一个使用普通main方法的应用程序,在我的IDE中作为普通的jar运行。它是一个spring-boot应用程序,默认情况下在嵌入式tomcat上运行 使用maven将其打包为war文件非常简单: <packaging>war</packaging> ... <dependency> <groupId>org.springframework.boot</groupId> <arti

我有一个使用普通main方法的应用程序,在我的IDE中作为普通的
jar
运行。它是一个
spring-boot
应用程序,默认情况下在嵌入式tomcat上运行

使用
maven
将其打包为
war
文件非常简单:

<packaging>war</packaging>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
<profiles>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <packaging.type>jar</packaging.type>
        </properties>
    </profile>

    <profile>
        <id>production</id>
        <properties>
            <packaging.type>war</packaging.type>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>
战争
...
org.springframework.boot
弹簧启动机tomcat
假如
问题:我仍然希望在我的嵌入式IDE中本地运行该应用程序。因此,排除上面的依赖项,并将打包留给jar好的,就这么简单:

<packaging>war</packaging>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
<profiles>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <packaging.type>jar</packaging.type>
        </properties>
    </profile>

    <profile>
        <id>production</id>
        <properties>
            <packaging.type>war</packaging.type>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

发展
符合事实的
罐子
生产
战争
org.springframework.boot
弹簧启动机tomcat
假如
+对于IntelliJ IDEA:运行适当的maven命令非常简单:检查所需的配置文件并单击“包”。

好的,它非常简单:

<packaging>war</packaging>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
<profiles>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <packaging.type>jar</packaging.type>
        </properties>
    </profile>

    <profile>
        <id>production</id>
        <properties>
            <packaging.type>war</packaging.type>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

发展
符合事实的
罐子
生产
战争
org.springframework.boot
弹簧启动机tomcat
假如

+对于IntelliJ IDEA:运行适当的maven命令非常简单:检查所需的配置文件,然后单击“包”。

Yes。Eclipse中有一个选项“profile”(如果您使用的是profile的话)。我将检查路径并回答问题。您不需要将其打包为可运行的
jar
。您还可以运行war,在它旁边,您仍然有main方法...@M.Deinum这将非常好,但是如果我将打包更改为
war
,我将得到以下异常:
线程“main”java.lang.NoClassDefFoundError中的异常:javax/servlet/Filter
,不管我是添加还是排除上面starter tomcat的dependency语句,这实际上是由于Intellij没有将
提供的
依赖项添加到类路径。当使用mvn springBoot:run
时,它会工作。您不想为不同的环境使用不同的工件/构建,最终会让您感到痛苦。(因此,如果您在生产环境中使用tomcat部署到imho,您也应该在本地进行部署,尤其是在这种情况下,由于资源的加载规则不同)。但我想继续运行应用程序,只需从gui中单击run,而不必使用maven。所以我可能不得不坚持下面的简介方法。是的。Eclipse中有一个选项“profile”(如果您使用的是profile的话)。我将检查路径并回答问题。您不需要将其打包为可运行的
jar
。您还可以运行war,在它旁边,您仍然有main方法...@M.Deinum这将非常好,但是如果我将打包更改为
war
,我将得到以下异常:
线程“main”java.lang.NoClassDefFoundError中的异常:javax/servlet/Filter
,不管我是添加还是排除上面starter tomcat的dependency语句,这实际上是由于Intellij没有将
提供的
依赖项添加到类路径。当使用mvn springBoot:run
时,它会工作。您不想为不同的环境使用不同的工件/构建,最终会让您感到痛苦。(因此,如果您在生产环境中使用tomcat部署到imho,您也应该在本地进行部署,尤其是在这种情况下,由于资源的加载规则不同)。但我想继续运行应用程序,只需从gui中单击run,而不必使用maven。因此,我可能必须坚持下面的配置文件方法。