Java Web 应用程序部署 Heroku

Java web application deployment Heroku

上次我正在研究可以为我的 Java Web 应用程序提供构建和部署的 Web 服务。我发现 Heroku 似乎还可以,但我遇到了一些问题。

首先,我的应用程序使用 Spring MVC(不是 Spring Boot),我有手动的基于 java 的注解配置,根本没有 web.xml。那是关于设置的。现在谈到提到的错误,我将 Heroku 连接到我的 GitHub 存储库,然后部署它。一切都很好,最后只有一个问题,如下所示:

2018-03-23T07:12:02.679019+00:00 app[web.1]: Error: Main method not found in class th.config.MyWebAppInitializer, please define the main method as:
2018-03-23T07:12:02.679037+00:00 app[web.1]: public static void main(String[] args)
2018-03-23T07:12:02.679150+00:00 app[web.1]: or a JavaFX application class must extend javafx.application.Application

显然 Heroku 环境需要一个包含 main 方法的 class,它在我的应用程序中不存在,至少在我自己的代码中不存在,但可能在运行 servlet 的 class 中某处,例如ServletInitializer 或类似的东西。我试图使用标志 <skipMain>true</skipMain> 标志来跳过这个但没有任何运气。

这里包含我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
....
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <skipMain>true</skipMain>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <executions>
                <execution>
                    <id>default-war</id>
                    <phase>prepare-package</phase>
                    <configuration>
                        <skipMain>true</skipMain>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>copy-dependencies</goal></goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.github.jsimone</groupId>
                                <artifactId>webapp-runner</artifactId>
                                <version>7.0.22.3</version>
                                <destFileName>webapp-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<groupId>webofficesuite</groupId>
<artifactId>webofficesuite</artifactId>
<version>1.0-SNAPSHOT</version>

<!-- SPRING COMPONENTS -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.13.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.13.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.13.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.13.RELEASE</version>
    </dependency>

    <!-- SPRING SECURITY -->
    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>


    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.3.13.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-dbcp</artifactId>
        <version>7.0.55</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-catalina</artifactId>
        <version>9.0.1</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/jstl/jstl -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

    ...Other dependencies

</dependencies>

如果能帮助我成功部署我的应用程序,我将不胜感激。此外,我尝试了其他名为 OpenShift 的 Web 服务,并设法在那里部署了我的应用程序,但我不喜欢它几乎不可能远程连接到 mysql 数据库,所以我更喜欢 Heroku 解决方案。但是,如果您有任何其他好的网络服务托管可以推荐,我愿意征求建议。唯一的要求是它每月不会花费太多钱,因为它不会是需要大量内核和内存的大型应用程序。

确保您的 Procfile 包含如下内容:

web: java -jar target/dependency/webapp-runner.jar --port $PORT target/*.war

您可以在 Deploying Tomcat-based Java Web Applications with Webapp Runner

上的 Heroku 文档中了解有关使用 Webapp Runner 的更多信息