如何通过cmd运行maven Web应用程序

时间:2018-04-11 12:23:23

标签: java maven tomcat web-applications

我是制作Web应用程序开发的新手,我正试图建立一个使用嵌入式Tomcat和maven播放视频的网站。我只有一个静态jsp文件,我可以在localhost上的intellij上运行程序,但是我发现使用命令行(mvn exec)在cmd中运行这个项目非常困难。我的主要目标是在我租用的服务器上运行此程序,然后使用我注册的域名访问该网站。这是我的pom.xml文件

的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>maven.movie.com</groupId>

 <artifactId>movieweb</artifactId>

 <version>1.0-SNAPSHOT</version>

 <packaging>war</packaging>

 <name>movieweb Maven Webapp</name>
 <!-- FIXME change it to the project's website -->
 <url>http://maven.apache.org</url>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.7</maven.compiler.source>
 <maven.compiler.target>1.7</maven.compiler.target>
 <tomcat.version>8.5.23</tomcat.version>
</properties>

<dependencies>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-core</artifactId>
  <version>${tomcat.version}</version>
</dependency>

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <version>${tomcat.version}</version>
</dependency>

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jasper</artifactId>
  <version>${tomcat.version}</version>
</dependency>

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jasper-el</artifactId>
  <version>${tomcat.version}</version>
</dependency>

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jsp-api</artifactId>
  <version>${tomcat.version}</version>
</dependency>

<dependency>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.6.0</version>
  <scope>provided</scope>
</dependency>

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

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

<dependency>
  <groupId>javax.el</groupId>
  <artifactId>javax.el-api</artifactId>
  <version>3.0.0</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.servlet.jsp.jstl</groupId>
  <artifactId>javax.servlet.jsp.jstl-api</artifactId>
  <version>1.2.1</version>
  <scope>compile</scope>
</dependency>

    movieweb            

    <plugin>
      <artifactId>maven-clean-plugin</artifactId>
      <version>3.0.0</version>
    </plugin>
    <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.0.2</version>
    </plugin>

    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
    </plugin>

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.20.1</version>
    </plugin>

    <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <version>3.2.0</version>
    </plugin>

    <plugin>
      <artifactId>maven-install-plugin</artifactId>
      <version>2.5.2</version>
    </plugin>

    <plugin>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>2.8.2</version>
    </plugin>

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>appassembler-maven-plugin</artifactId>
      <version>2.0.0</version>
      <configuration>
        <assembleDirectory>target</assembleDirectory>
        <programs>
          <program>
            <mainClass>launch.Main</mainClass>
            <name>webapp</name>
          </program>
        </programs>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>assemble</goal>
          </goals>
        </execution>
      </executions>
    </plugin>

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.6.0</version>
      <configuration>
        <mainClass>launch.Main</mainClass>
      </configuration>
    </plugin>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.4</version>
      <configuration>
        <archive>
          <manifest>
            <mainClass>launch.Main</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>

  </plugins>
</pluginManagement>

2 个答案:

答案 0 :(得分:3)

将此依赖项添加到您的pom.xml

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <port>9090</port>
      <path>/</path>
    </configuration>
</plugin>

然后你可以像这样运行这个新插件提供的maven任务

mvn tomcat7:run

答案 1 :(得分:0)

@ piy26
谢谢你的解决方案。
但是,当我们想要在未安装Web服务器的本地测试应用程序时,它非常有用。

@Karan,对于当地人,您可以使用以上解决方案 但是为了在服务器上运行代码,我建议运行 mvn clean package 命令,如pom.xml中所述,它将构建一个战争,你可以保持在Web服务器目录中并启动Web服务器 这必须在您租用的服务器上完成。我建议暂时使用AWS免费套餐。

我知道这是一个漫长的过程,为了避免这种情况,您可以使用Spring Boot,无需部署war文件嵌入Web服务器。

希望这会有所帮助:)