首页 文章

使用maven转换现有的Web项目

提问于
浏览
0

我一直在尝试将生成war文件的Web项目转换为maven . 我现有的项目结构如下 -

MyWebProject | - WEB-INF / src - 包含一堆软件包,如com.myweb.client,com.myweb.server等 - WEB-INF / test - 包含1个软件包com.myweb.tests - web-scripts - 包含一堆脚本(只是一个文件夹;不在类路径上) - misc-files1 - 包含misc文件集1 - misc-files2 - 类似于上面

目前正在使用ant脚本创建war文件,其结果是war文件结构,如下所示

myweb.war - Meta-INF(仅包含MANIFEST.MF) - WEB-INF - 类 - com.myweb.client - com.myweb.server等 - web-scripts - misc-files1 - misc-files2

我使用简单工件创建了一个基本的maven项目并添加了我的包 . 我正在使用maven程序集插件生成war文件并使用filesets进行过滤 . 但是我的战争文件并没有接近我用 Ant 得到的东西 . 这是我的assembly.xml文件的缩短版本

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

  <id>assembler</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
<dependencySet/>
</dependencySets>

<fileSets>

    <fileSet>
        <directory>src/main/java</directory>
        <outputDirectory>WEB-INF</outputDirectory>
        <includes>
        <include>**</include>
        </includes>        
    </fileSet>

    <fileSet>
        <directory>es</directory>
        <outputDirectory>resources1</outputDirectory>
        <includes>
        <include>**</include>
        </includes>        
    </fileSet>
    <fileSet>
        <directory>resources2</directory>
        <outputDirectory>resources2</outputDirectory>
        <includes>
        <include>**/*</include>
        </includes>        
    </fileSet>
    <fileSet>           
    <fileSet>
        <directory>test</directory>
        <outputDirectory>WEB-INF</outputDirectory>
        <includes>
        <include>**</include>
        </includes>        
    </fileSet>

  </fileSets>


</assembly>

要创建自定义战争,装配插件是正确的方法 . 我总是可以使用maven-antrun-plugin在pom中包含我的ant脚本,但是如果可能的话我想避免使用ant . 有什么建议 .

2 回答

  • 0

    你应该使用maven-war-plugin .

  • 0

    你更舒服,首先让Maven处理开箱即用的所有东西,比如编译,测试和创建WAR . 尝试将依赖关系管理移动到Maven . 如果仍需要在Ant中设置类路径,请使用Maven Ant tasks让Maven为您的Ant脚本构建类路径 . 在Ant构建文件中保留任何自定义内容并使用Maven中的antrun plugin调用它,除非您转换为使用Maven插件 .

    Edit: 您的问题似乎是您有多个目录包含需要捆绑到战争中的源文件 . 是对的吗?最简单的解决方案是将它们全部放在一个地方 - src / main / webapp是the Maven convention . 即使是Ant构建也应该至少拥有这么多组织 . 如果有's a really good reason to have your war resources scattered all over (I'喜欢听到它,那么您可以使用其他插件,如resources pluginGMavenantrun plugin将多个目录复制到war插件构建war目录结构的目录中 . 这被称为"webappDirectory" in the war plugin . 使用antrun插件的可能性可以追溯到我从Maven调用现有Ant脚本片段的原始答案 . 如果这个多目录的东西必须保留并且已经由Ant处理,那么将它重构为也可以从Maven使用的目标/任务 . 尽可能让您在采用Maven时重复使用现有构建的自定义部分 .

相关问题