如何使用Maven构建war包并使用Jenkins部署到Tomcat服务器。

huangapple go评论53阅读模式
英文:

How to use Maven to build a war and use Jenkins to deploy to Tomcat server

问题

我已经创建了一个Servlet项目。项目结构如下:

.
 |-- pom.xml
 |-- web
 |   |-- css
 |   |-- fonts
 |   |-- image
 |   |-- js
 |   |-- css
 |   |-- WEB-INF
 |   |   `-- web.xml
 |   `-- index.jsp
 `-- src
     `-- MyServlet.java

而我的 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>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <finalName>web-app-name</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <webXml>web\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

当我尝试运行 mvn war:war 时,我发现它在目标文件夹下创建了一个新的 example.war 文件。然而,这个 example.war 文件是空的

我想要做的是创建一个 *.war 文件,并通过使用 Jenkins 将这个 *.war 文件部署到 Tomcat。有人知道如何做吗?谢谢!

英文:

I hava create a servlet project. The project structure is like that:

.
 |-- pom.xml
 |-- web
 |   |-- css
 |   |-- fonts
 |   |-- image
 |   |-- js
 |   |-- css
 |   |-- WEB-INF
 |   |   `-- web.xml
 |   `-- index.jsp
 `-- src
     `-- MyServlet.java

And my pom.xml is:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.example&lt;/groupId&gt;
    &lt;artifactId&gt;example&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;build&gt;
        &lt;finalName&gt;web-app-name&lt;/finalName&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.2&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;webXml&gt;web\WEB-INF\web.xml&lt;/webXml&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

When I try to run mvn war:war, I found it create a new example.war under target folder. However, the example.war is empty.

What I want to do is to create a *.war and deploy this *.war to Tomcat by using Jenkins. Anyone know how to do it? Thanks!

答案1

得分: 1

我认为你没有遵循正确的项目结构。看起来你需要:

项目名
|-- pom.xml
`-- src
    `-- main
        `-- webapp
            |-- WEB-INF
            |   `-- web.xml
            `-- index.jsp
        `-- java

查看Maven War文件原型

英文:

I don't think you're following the correct project structure. It looks like you need:

project
|-- pom.xml
`-- src
    `-- main
        `-- webapp
            |-- WEB-INF
            |   `-- web.xml
            `-- index.jsp
        `-- java

See the Maven Archetype for War files

答案2

得分: 0

跟随Ashley的建议。

此外,使用mvn clean verify构建你的项目。单独运行war插件不起作用。

英文:

Follow Ashley's advice.

Furthermore, build your project with mvn clean verify. Running the war plugin separately does not work.

huangapple
  • 本文由 发表于 2020年9月17日 23:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63941761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定