部署简单的Java Spark Web应用程序

时间:2016-04-24 18:06:08

标签: java maven deployment web-deployment

我试过这个简单的" Hello World"在Setting up Spark with Maven中的Web应用程序(使用maven设置),在Windows平台中使用Eclipse。如果它是从Eclipse运行的话它可以很好地工作。

但我想要实现的是让它在我在云端的CentOS 6.x中运行,我使用putty通过ssh访问它。我已经在CentOS中安装了JDK 8和maven。我没有安装任何Application Server(没有Tomcat等),因为我希望Spark应用程序可以使用其嵌入式AS(即Jetty)运行。

我还没有找到如何将这样一个Spark应用程序从Eclipse / Maven / Windows部署到云端的CentOS服务器。我发现了一些教程,解释了如何在heraku上进行部署,甚至是如何在不同的AS中部署它(不使用嵌入式的AS),但没有像我想要的那样简单,正如所解释的那样。

在Eclipse中,如果我将项目导出为jar,并将jar上传到CentOS服务器,则使用' java -jar appname.jar'运行它。不起作用,似乎它没有Spark库,也没有嵌入式Jetty。

有任何帮助吗?感谢。

1 个答案:

答案 0 :(得分:2)

After watching a video tutorial about maven, I have found the solution. First, you need to add the following to the xml.pom file in your maven project in Eclipse:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <!-- This tells Maven to include all dependencies -->
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>chatapp.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

It is important to tell maven where to find the main class. In my case, I named my app chatapp (because I'm planning to evolve this simple 'Hello World' app into a chat app), and the Main class containing the main method is also named Main, which is under the package chatapp, thus it is chatapp.Main. You can add these lines right after the <dependencies> node. Therefore the pom file should look like this:

 <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>com.orboan</groupId>
<artifactId>chatapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>new-spark-project</name>
<dependencies>
    <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <!-- This tells Maven to include all dependencies -->
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>chatapp.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Now, the important thing is NOT to use the export utility in Eclipse (right click on the project in the package explorer and from contextual menu do not choose Export) as you usually do when you want to export a java project into a jar file for deployment. This will generate a jar file, but without all dependencies needed.

Instead, you have to choose (from the contextual menu) Run As > Maven Install. This will generate (from the pom.xml) the corresponding jar file with all needed dependencies included, in my case chatapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar.

Then, upload that jar file to the CentOS server and just run it with java -jar
chatapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar
. It should work, now.

相关问题