【问题标题】:How to run app as jar in IDE, but package with maven as war?如何在IDE中将应用程序作为jar运行,但将maven打包为war?
【发布时间】:2017-11-14 20:38:22
【问题描述】:

我有一个具有普通 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>

问题:我仍然想在我嵌入的 IDE 中本地运行该应用程序。因此排除上面的依赖,将包装留给&gt;jar&lt;

问题:是否可以例如在 maven 中定义配置文件,以便默认情况下打包为 =jar,但在运行特定配置文件时,它会与 war 交换并包含 tomcat 服务器依赖项?

【问题讨论】:

  • 是的。 Eclipse 中有一个“配置文件”选项(如果您正在使用该选项)。我会检查路径并回答问题。
  • 您无需将其打包为jar 即可运行。你也可以运行war,旁边还有main方法...
  • @M.Deinum 那太好了,但是如果我将包装更改为war,我会得到以下异常:Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/Filter,无论我添加还是排除依赖语句对于上面的 starter-tomcat。
  • 这实际上是由于 Intellij 没有将 provided 依赖项添加到类路径中。当使用mvn springBoot:run 时,它会起作用。你不想为不同的环境使用不同的工件/构建最终会咬你。 (因此,如果您在生产中使用 tomcat 部署到 imho,您也应该在本地执行此操作,在这种情况下,尤其是由于资源的加载规则不同)。
  • 好的,我明白了,谢谢。但我想继续运行应用程序,只需从 gui 中单击运行,无需 maven。所以我可能不得不坚持下面的配置文件方法。

标签: java spring maven tomcat spring-boot


【解决方案1】:

好的,就这么简单:

<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>

+for IntelliJ IDEA:运行适当的 maven 命令很简单:检查所需的配置文件并单击“包”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2011-01-11
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    相关资源
    最近更新 更多