在Intellij上运行但不在Tomcat上运行的Spring Boot 2 Maven项目

时间:2018-10-25 12:31:42

标签: java spring maven spring-boot tomcat

我一直在尝试这一尝试,但并没有取得太大的成功,因此,任何暗示解决方案的线索将不胜感激。我根本不是Maven专家,所以这可能是主要问题。

我现在有一个SpringBoot后端,如果我从Intellij SpringBoot运行它,它可以完美运行。但是,如果我 mvn全新安装以创建要在 Tomcat8.5 中部署的War程序包,则对于所有路由,都会出现 404错误尝试过的。

这是父目录的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>eu.sacs</groupId>
<artifactId>sacs-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>Parent - SACS for CORDIS project</description>

<modules>
    <module>sacs-knowledge-extraction</module>
    <module>sacs-repository-oracle</module>
    <module>sacs-service</module>
    <module>sacs-webservice</module>
</modules>

<prerequisites>
    <maven>3</maven>
</prerequisites>

<properties>
    <framework.version>0.0.0</framework.version>
    <java.version>1.10</java.version>
    <repo.path></repo.path>
</properties>

<dependencyManagement>
    <dependencies>
        <!-- Module dependencies -->
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-webservice</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-service</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-repository-oracle</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-knowledge-extraction</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- Apache Jena -->
        <dependency>
            <groupId>org.apache.jena</groupId>
            <artifactId>jena-core</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!-- Apache POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

        <!-- Database - Oracle -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.6.0</version>
        </dependency>


        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>1.1.0</version>
            <scope>test</scope>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

        <!-- Excel -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>


<repositories>
    <repository>
        <id>sacs</id>
        <url>file://${repo.path}</url>
    </repository>
</repositories>

<pluginRepositories>

    <pluginRepository>
        <id>sacs</id>
        <url>file://${repo.path}</url>
    </pluginRepository>

</pluginRepositories>

<build>
    <!-- <finalName>${warname}</finalName> -->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.5.RELEASE</version>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <outputDirectory>.</outputDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.10</source>
                <target>1.10</target>
            </configuration>
        </plugin>
    </plugins>
</build>

这是用于Web服务的一个(其他模块被编译到用于Web服务的jar中)

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>eu.sacs</groupId>
    <artifactId>sacs-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>sacs-webservice</artifactId>
<packaging>war</packaging>

<name>${project.artifactId}</name>
<description>Rest API and Controller - everis sacs for CORDIS project</description>

<dependencies>
    <dependency>
        <groupId>eu.sacs</groupId>
        <artifactId>sacs-service</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-cas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
    </dependency>
</dependencies>

最后,这是Tomcat日志的相关部分:

25-Oct-2018 12:58:15.012 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\apache-tomcat-8.5.34\webapps\sacs-webservice-1.0-SNAPSHOT.war]
25-Oct-2018 12:58:23.167 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
25-Oct-2018 12:58:23.636 WARNING [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [216] milliseconds.
25-Oct-2018 12:58:23.677 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\apache-tomcat-8.5.34\webapps\sacs-webservice-1.0-SNAPSHOT.war] has finished in [8,665] ms

2 个答案:

答案 0 :(得分:2)

在您的日志中部署sacs-webservice-1.0-SNAPSHOT.war期间没有错误。默认情况下,WAR名称是部署后的上下文路径,因此在您的情况下,示例URL将类似于http://localhost:8080/sacs-webservice-1.0-SNAPSHOT/index.html

在IntelliJ中运行时,WAR文件名不是上下文路径的一部分,因此示例URL看起来像http://localhost:8080/index.html。这将进一步取决于IntelliJ中的“运行配置”,但是对于mvn clean package spring-boot:run命令来说通常是正确的。

最重要的是,由于您选择使用独立的Tomcat,因此URL不同。

答案 1 :(得分:0)

好吧,终于设法遵循the documentation来弄清楚了。

基本上,我必须使用SpringBootServletInitializer扩展我的Main类,覆盖configure(SpringApplicationBuilder application),最后,也许也是最重要的一点,添加此缺少Maven依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

然后一切都会正常进行。