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

Java web应用程序部署Heroku

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

    上一次我在做一些关于可以提供build&的web服务的研究;部署Java 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方法的类,该类不存在于我的应用程序中,至少不存在于我自己的代码中,但可能存在于运行servlet的类的某个地方,如 ServletInitializer 或者类似的东西。我试图使用flag <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解决方案。但是,如果有任何其他好的web服务宿主,您会推荐我,我欢迎您的建议。唯一的要求是,它每月的成本不会高得离谱,因为它不会是需要大量内核和内存的大型应用程序。

    1 回复  |  直到 7 年前
        1
  •  0
  •   codefinger    7 年前

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

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

    您可以在Heroku文档中了解有关使用Webapp Runner的更多信息 Deploying Tomcat-based Java Web Applications with Webapp Runner