java - 如何使用 Maven 创建最小 Java 应用程序的框架?

标签 java maven servlets

<分区>

出于测试目的,我需要创建一个非常简单的 Java 网络应用程序,它将只包含一个 servlet。

我希望能够轻松运行此 Web 应用程序(例如,通过输入 mvn jetty:run)。

对于 Vaadin 应用程序,我使用了这些调用:

1) mvn archetype:generate
-DarchetypeGroupId=com.vaadin
-DarchetypeArtifactId=vaadin-archetype-clean
-DarchetypeVersion=LATEST
-DgroupId=your.company
-DartifactId=project-name
-Dversion=1.0
-Dpackaging=war

2) mvn eclipse:eclipse

现在我需要类似的调用来创建一个只有一个 servlet 的简单网络应用程序。

我该怎么做(我可以使用哪个 Maven 命令/原型(prototype))?


解决方案

为了创建网络应用程序的骨架,请执行以下步骤:

1)运行

mvn archetype:generate -DgroupId=ru.mycompany -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2)放

<plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>7.0.0.pre5</version>
        <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
        </configuration>
        <executions>
                <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                                <goal>run</goal>
                        </goals>
                        <configuration>
                                <scanIntervalSeconds>0</scanIntervalSeconds>
                                <daemon>true</daemon>
                        </configuration>
                </execution>
                <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                                <goal>stop</goal>
                        </goals>
                </execution>
        </executions>
</plugin>

进入 pom.xml 文件,如下所示:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ru.mycompany</groupId>
    <artifactId>mywebapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>mywebapp Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>mywebapp</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>maven-jetty-plugin</artifactId>
                    <version>7.0.0.pre5</version>
                    <configuration>
                        <scanIntervalSeconds>10</scanIntervalSeconds>
                        <stopKey>foo</stopKey>
                        <stopPort>9999</stopPort>
                    </configuration>
                    <executions>
                        <execution>
                            <id>start-jetty</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <scanIntervalSeconds>0</scanIntervalSeconds>
                                <daemon>true</daemon>
                            </configuration>
                        </execution>
                        <execution>
                            <id>stop-jetty</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>stop</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

3) 运行 mvn jetty:run -Djetty.port=PORT 并确认您在浏览器中看到了 Hello World! 字样(localhost:PORT/mywebapp),其中 PORT 是您希望 Jetty 运行的端口号。

最佳答案

您需要使用 -DarchetypeArtifactId=maven-archetype-webapp 创建 archtype

此外,要使 jetty:run 正常工作,您需要使用 jetty plugin 更新 pom.xml

<plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>7.0.0.pre5</version>
        <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
        </configuration>
        <executions>
                <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                                <goal>run</goal>
                        </goals>
                        <configuration>
                                <scanIntervalSeconds>0</scanIntervalSeconds>
                                <daemon>true</daemon>
                        </configuration>
                </execution>
                <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                                <goal>stop</goal>
                        </goals>
                </execution>
        </executions>
</plugin>

关于java - 如何使用 Maven 创建最小 Java 应用程序的框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18780839/

相关文章:

Java:使用java命令解压rar文件

java - Eclipse SWT 小部件 : Combining two Listeners

java - 将插件资源文件分发到宿主项目

java - 单击提交按钮时出现 Http404 错误(Servlet 问题)

java - 是否可以从 Servlet 中调用一个独立的线程?

java - 比较两个 arrayList 以匹配后处理的条件

java - 向字符串数字添加零的有效方法?

maven-2 - maven3 运行时可以执行 maven2 兼容的 pom.xml 文件吗

maven - 由于缺少 http ://jetty. mortbay.org/configure.dtd,故障保存无法启动 jetty 服务器

java - GWT - Servlet 数据库访问