zl程序教程

您现在的位置是:首页 >  后端

当前栏目

普通web项目转化为maven web项目

Maven项目Web 转化 普通
2023-09-14 09:00:23 时间
1..configure - Convert to Maven Project



2..maven添加jetty支持 ,并且修改webAppSourceDirectory

         !--  添加jetty支持,Jetty 8 必须 Jdk 1.6+,Servlet 3.0,类似于 Tomcat 7--

         plugin
          groupId org.mortbay.jetty /groupId
          artifactId jetty-maven-plugin /artifactId
          version 8.1.16.v20140903 /version
          configuration
             webAppSourceDirectory ${basedir}/WebContent /webAppSourceDirectory
          /configuration
         /plugin



重要:maven项目webAppSourceDirectory默认为src/main/webapp对应于普通web项目的webcontent目录

mvn jetty:run-war  先打包,然后再部署(只打成war包的话也可以用mvn package命令)

mvn jetty:run -Djetty.port=80  默认端口也为8080


3.转换为maven项目jetty运行乱码问题(maven打包时候系统默认编码为 gbk)
pom.xml添加下面两个plugin
  plugin
groupId org.apache.maven.plugins /groupId
artifactId maven-resources-plugin /artifactId
configuration
encoding UTF-8 /encoding
/configuration
/plugin

plugin
artifactId maven-compiler-plugin /artifactId
configuration
encoding UTF-8 /encoding
/configuration
/plugin





${basedir} represents the directory containing pom.xml


4.添加junit依赖
  dependencies
  dependency
      groupId junit /groupId
      artifactId junit /artifactId
     version 4.11 /version
    /dependency


  /dependencies

5.添加服务器相关jar包

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


6.添加json所需依赖(Jackson三个主要的模块:缺少这些jar包无法将对象转成json)

fasterxml为2.x(新版spring用的是这个)     1.x版本的包名是codehaus
!--对象转json所需jar包  --
     dependency  
            groupId com.fasterxml.jackson.core /groupId  
            artifactId jackson-core /artifactId  
            version 2.1.0 /version  
        /dependency  
        dependency  
            groupId com.fasterxml.jackson.core /groupId  
            artifactId jackson-databind /artifactId  
            version 2.1.0 /version  
        /dependency  
        dependency  
            groupId com.fasterxml.jackson.core /groupId  
            artifactId jackson-annotations /artifactId  
            version 2.1.0 /version  
        /dependency
    

7.依赖jar包放在WebContent/WEB-INF/lib等目录下的情况

配置编译参数 compilerArguments ,添加extdirs将目录下的jar包相对路径添加到配置中,如下:

     build
        plugins
            plugin
              artifactId maven-compiler-plugin /artifactId
              configuration
                  source 1.7 /source
                  target 1.7 /target
                  encoding UTF-8 /encoding
                  compilerArguments
                   extdirs WebContent\WEB-INF\lib /extdirs
                 /compilerArguments
              /configuration
            /plugin
        /plugins
    /build