1. 描述
最近一直在忙于Ant项目升级Maven的工作,在升级的过程中遇到了很多的错误。一路坎坎坷坷,今天终于交接完毕了。由于项目托管是使用GitLab CI(Continuous integration,简称CI),因此打包环境和本地有所不同。GitLab CI是GitLab的一部分,负责管理项目和用户界面,并允许对每次提交进行单元测试,在构建失败时显示警告消息。每当开发人员将代码推送到应用程序时,它都会构建和测试软件。 由于本人对Ant和Maven用的不是非常熟,所以在升级的过程碰壁很多,希望大家在升级的过程中对比Ant和Maven的不同之处,熟悉Maven结构插件,并学会基础的命令打包再进行升级操作。
2. 分析
首先了解Maven结构目录,Maven结构目录与普通的Web项目不同。
Maven结构如下:
升级项目结构如下:
清楚了Maven结构,再进行对比升级的项目,找出各自对应的目录存放位置。首先分析这次升级的目录结构,发现是有些类似Maven的。其中src/**/java
相当于Maven的src/main/java
,web
相当于Maven的src/main/webapp
。Eclipse在创建Maven项目时,默认是WebContent
作为web
,src/main/webapp
没有被创建。如果需要使用src/main/webapp
作为web
目录,可以在src目录下自行创建目录,右键webapp文件夹->Build Path->Use as source folder
即可作为web目录。
创建src/main/webapp
作为web
目录:
3、升级
1)、 ant项目转maven
选中项目右键->Configure->Convert to Maven Project
2)、 删除ant项目转maven操作之后自动新建的WebContent目录文件夹,这里使用的是web目录
3)、 删除自动新建resources文件夹
4)、删除lib下的所有jar包
首先备份所有jar,然后再删除项目下的jar,编辑pom.xml文件。
5)、在pom.xml中配置jar包依赖和插件
配置依赖jar是很费心思和时间的一项任务,需要对比本地lib版本号找对应的依赖,版本依赖不兼容可能会导致诸多问题发生,所以这一步需要细心耐心,避免不必要的麻烦。在https://mvnrepository.com/可以找到对应的dependency,直接复制对应版本的jar,无需自己手写配置。本地maven中央仓库所有jar包必须存有**.pom、.pom.sha1、.jar、**.jar.sha1文件,如果后缀存在.lastUpdated,说明没有下载成功,pom.xml运行会报错。由于有些repository并未收录,所以就肯定找不到这个.pom文件。依赖配置完,开始配置插件。
所需插件:
maven-war-plugin
maven-war-plugin主要用在打war包时使用,这里指定webResources
的directory
为web/
,主要是动态交互页面为主。filtering
为false表示不过滤web/下的文件,为true表示过滤。当设置为true时,pom.xml配置的本地jar包引用会失效,下载的jar为pom.xml中定义的空文件,这在启动weblogic时是一个很严重的问题(过来人的深刻体验…)。packagingExcludes
的作用是排除jar包,failOnMissingWebXml
表示项目中不需web.xml,没有web.xml打包时会报web.xml is missing and 'failOnMissingWebXml' is set to true
。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>web/</directory>
<filtering>false</filtering>
<includes>
<include>*.*</include>
<include>test1/</include>
<include>test2/</include>
<include>WEB-INF/</include>
</includes>
</resource>
</webResources>
<packagingExcludes>
WEB-INF/lib/xml-apis-1.3.04.jar,
WEB-INF/lib/xercesImpl-2.8.1.jar
</packagingExcludes>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
maven-compiler-plugin
maven-compiler-plugin主要用于编译class文件,定义编译版本。
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-verbose</arg>
<arg>-Xlint:all,-options,-path</arg>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
<compilerVersion>1.8</compilerVersion>
</configuration>
</plugin>
本次production打包测试环境是GitLab CI,使用定义的.gitlab-ci.yml
进行打包。在交接测试时,本地测试完全正确,但是在GitLab CI始终没有class文件编译成功,打包却是正常的。java文件编译依赖于jdk,如果不配置则无法编译。几天的思考讨论,同事和我一致认为是对方的环境问题。查看.gitlab-ci.yml
,发现一处问题。
image: maven:latest
指定的是最新版本,并没有指定java版本。如果对方平台使用的是java9,那么与项目编译插件中配置1.8不匹配。这个问题真是深藏不露,很难了解到。解决办法是更改为image: maven:3-jdk-8
即可正常编译。
.gitlab-ci.yml:
image: maven:latest
variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
MAVEN_REPO: "$CI_PROJECT_DIR/.m2"
cache:
paths:
- .m2/repository/
stages:
- compile
- build
- test
- report
compile:
stage: compile
script:
- mvn $MAVEN_CLI_OPTS clean install compile
artifacts:
paths:
- target/
build:
stage: build
script:
- mvn war:war
artifacts:
paths:
- target/
maven-clean-plugin
主要用于清除上次打包的缓存
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
<configuration>
<failOnError>false</failOnError>
</configuration>
</plugin>
build-helper-maven-plugin
该插件非常重要,决定整个项目要打包的所有资源文件。这里定义了两个execution
,add-resource
和add-source
。add-resource
用于增加自定义配置文件,无需放在src/main/resource
中也可在自动在打包时编译到WEB-INF/classes
路径下。add-source
用于配置多个子目录,解决默认只打包设置为source的目录困扰。一个项目有多个子功能模块,如果所有的功能模块都设置为source,那么在项目自动编译时是很耗时的事。在平时编程中,只需选择部分子模块设置为source即可,在编译时才让所有子模块编译打包。outputDirectory
是src目录文件下编译后的存储路径。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<!-- 添加主资源文件目录 -->
<executions>
<execution>
<!--自定义名称,不可重复 -->
<id>add-resource</id>
<!--指定绑定到生命周期 -->
<phase>initialize</phase>
<!--指定指定的目标,可添加多个 -->
<goals>
<goal>add-resource</goal>
</goals>
</execution>
<execution>
<id>add-source</id>
<phase>initialize</phase>
<goals>
<goal>add-source</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${pom.basedir}/target/classes</outputDirectory>
<resources>
<!--资源文件目录,可添加多个 -->
<resource>
<directory>${pom.basedir}/src/base/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.xls</include>
<include>**/*.xlsx</include>
<include>**/*.jpg</include>
<include>**/*.ftl</include>
</includes>
</resource>
</resources>
<!-- 添加主源码目录 -->
<sources>
<source>${pom.basedir}/src/base/java</source>
<source>${pom.basedir}/src/module1/java</source>
<source>${pom.basedir}/src/module2/java</source>
</sources>
</configuration>
</plugin>
6)、引入jar报错解决方法
方式一、:在pom.xml配置本地jar
下载失败的jar可以配置在lib目录中,并在pom中引入
<dependency>
<groupId>org.jbarcode</groupId>
<artifactId>jbarcode-0.2.8</artifactId>
<version>0.2.8</version>
<scope>system</scope>
<systemPath>${pom.basedir}/web/WEB-INF/lib/jbarcode-0.2.8.jar</systemPath>
</dependency>
方式二、删除文件夹,重新下载
方式三、配置maven仓库的settings.xml,添加多个国内镜像,
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
Tips:阿里云镜像下载速度快,有些maven官网下载失败的也可以下载,不过大部分使用官网的,如果项目迁移到他处,兼容性可能差一些。
方式四、在maven官网手动下载所需文件夹,点击View All既可以看见jar与.pom等文件。
这种方式不太推荐,可移植性较差,不同机都需要自己下载jar,易出错!
4、异常总结
1、警告:xxxx 是 Sun 的专用 API,可能会在未来版本中删除
事实上,sun.misc包都是sun公司的内部类,并没有在java api中公开过,不建议使用,所以使用这些方法是不安全的,
将来随时可能会从中去除,所以相应的应该使用替代的对象及方法
方法一、使用org.apache.commons.codec.binary.Base64
类,commons-codec.jar
方法二、在pom.xml配置如下,新增一个rt.jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
2、编译时 error: package junit.framework does not exist
在pom.xml中加入:
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
</dependency>
3、maven插件不执行-build-helper-maven-plugin pom中不执行
pom文件中删除围绕插件标签。
4、 Unrecognised tag: 'execution’
execution和其他的标签位置放错,这里应该查看build-helper-maven-plugin.pom,看它的配置是如何配的
5、File encoding has not been set, using platform encoding
配置
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6、package javax.crypto does not exist
加上${java.home}/lib/jce.jar
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-extdirs</arg>
<arg>${basedir}/WEB-INF/lib</arg>
</compilerArgs>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
7、org.apache.axis.utils does not exist
导入axis-1.4.jar
8、error: cannot find symbol
有引用不存在类或者字符写错
9、Unrecognised tag: ‘repositories’(position: START_TAG seen … -->\n\t… @125:16) @ C:\Users\nicoletang.m2\settings.xml, line 125, column 16
缺少profiles
标签
10、ant build错误checking for updates from apache.snapshots
.m2中的settngs.xml的<snapshots> <enabled>true</enabled> </snapshots>
设置为true
11、ant识别不了pom.xml,报package org.apache.commons.configuration does not exist
配置错误,正确配置如下:
<target name="init" depends="clean">
<echo message="*****初始化****" />
<path id="maven-ant-tasks.classpath" path="${lib.dir}/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant" classpathref="maven-ant-tasks.classpath" />
<artifact:pom id="maven.project" file="pom.xml" />
<artifact:dependencies pathId="maven.classpath" filesetid="maven.fileset">
<pom refid="maven.project" />
</artifact:dependencies>
<copy todir="${lib.dir}">
<fileset refid="maven.fileset" />
<mapper type="flatten" />
</copy>
<echo message="*****初始化,下载maven库结束****" />
</target>
12、ant编译找不到符号错误
当运行javac命令对项目编译时,肯定需要依赖项目的jar包,此外还要引用一些jdk的jar包,配置<compilerarg value="-XDignore.symbol.file" />
同时引用class.path
<javac srcdir="${module.name}/${src.dir}" destdir="${dest.dir}" includeantruntime="false" debug="true" encoding="UTF-8" classpathref="fas-classpath">
<compilerarg value="-XDignore.symbol.file" />
<compilerarg value="-Xlint:unchecked" />
<compilerarg value="-Xlint:deprecation" />
<classpath refid="class.path" />
</javac>
<path id="class.path">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
13、 deps.fileset.compile doesn’t denote a fileset
build.xml中复制路径错误错误,应该是filesetid
14、maven项目 加入json-lib-2.2.3-jdk15.jar 报错 Missing artifact net.sf.json-lib:json-lib:jar:2.4:compile
json-lib是需要区分jdk版本的,pom.xml中的配置应加上标签classifier指定jdk版本,如用jdk15,就正常了。
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
<version>2.2.3</version>
</dependency>
15、System.out.println(Util.class.getClassLoader().getResource("/").getPath());
报空指针异常**
resources目录下存放的资源文件经过编译后直接放到了项目根目录下了,正确获取方式:
Properties prop = new properties();
prop.load(this.getClass().getResourceAsStream("/test.properties"));
16、error: package org.eclipse.jdt.internal.compiler.ast does not exist
\workspace\fas2\src\oms\java\oms\com\hgc\oms\inventory_system\util\MyHtmlUtil.java:[9,44] error: package org.eclipse.jdt.internal.compiler.ast does not exist
pom.xml导入:
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.5.1</version>
</dependency>
17、Message icon - Error javax.xml.parsers.DocumentBuilderFactory: Provider
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not a subtype
正常的情况下,会优先使用你的配置类。你可以在 systemProperty中配置具体使用哪个类。
如果没有配置,会使用 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
的实现类,是jdk8中自带的实现类。
默认的 fallback类为:com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
通过点击DocumentBuilderFactory
类查看,发现调用的是rt.jar里的类,包为javax.xml.parsers,
这里需要下载xerces.jar
,其中包下的javax.xml.parsers.DocumentBuilderFactory
正是需要的类
导入:
<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
<version>1.4.4</version>
</dependency>
然后在maven-war-plugin
插件中排除:
<configuration>
<packagingExcludes>
WEB-INF/lib/xml-apis-1.3.04.jar,
WEB-INF/lib/xercesImpl-2.8.1.jar
<packagingExcludes>
</configuration>
18、web.xml is missing and is set to true
配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
19、java.lang.ClassNotFoundException: javax.ws.rs.client.RxInvokerProvider
低版本没有这个类,升级版本为javax.ws.rs-api-2.1.1.jar
20、java.lang.IllegalStateException: InjectionManagerFactory not found.
具体原因是缺少jar包,解决办法是在maven配置文件pom.xml中加入依赖,或者直接到官网下载对应jar包:
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
21、ValidationAutoDiscoverable cannot be cast to org.glassfish.jersey.internal.spi.AutoDiscoverable
导入
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.26</version>
</dependency>
22、Message icon - Error org.glassfish.jersey.jsonp.internal.JsonProcessingAutoDiscoverable cannot be cast to org.glassfish.jersey.internal.spi.AutoDiscoverable
导入
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.26</version>
</dependency>
23、invalid LOC header (bad signature)
重新删除jar再下载
24、Neither nor can be specified
when is turned on in weblogic.xml
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
上面配置的含义:优先使用Web应用里加载的类,即就是优先加载web-inf下lib中的jar包。在weblogic部署过程中,发现jersey包总是找不到,需要导入很多jar才行。这里可以直接在weblogic.xml配置prefer-application-packages
,可以避免导入过多的jar包,消灭包找不到异常。
如下配置:
<container-descriptor>
<!-- <prefer-web-inf-classes>true</prefer-web-inf-classes>-->
<index-directory-enabled>true</index-directory-enabled>
<show-archived-real-path-enabled>true</show-archived-real-path-enabled>
<prefer-application-packages>
<package-name>com.sun.jersey.*</package-name>
<package-name>com.sun.research.ws.wadl.*</package-name>
<package-name>com.sun.ws.rs.ext.*</package-name>
<package-name>org.codehaus.jackson.*</package-name>
<package-name>org.codehaus.jettison.*</package-name>
<!-- <package-name>javax.ws.rs.*</package-name> -->
<package-name>org.objectweb.asm.*</package-name>
<package-name>org.joda.*</package-name>
</prefer-application-packages>
</container-descriptor>
25、java获取资源文件通过getClass().getClassLoader().getResource("/").getPath()
编译后的路径(即classes路径),Web项目写法是正确的,但是在Maven中会报空指针异常,
这里获取资源路径改为this.getClass().getResourceAsStream("/*.properties")
26、web.xml文件读取src/main/resourse格式:
动态web项目web.xml:
<servlet>
<servlet-name>TestManager</servlet-name>
<servlet-class>test.TestManager</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/test.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
maven项目web.xml:
<servlet>
<servlet-name>TestManager</servlet-name>
<servlet-class>test.TestManager</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>classpath:test.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
27、maven build The scm url cannot be null
在pom.xml配置
<scm>
<connection>scm:svn:http://code.taobao.org/svn/demo_</connection>
<developerConnection>scm:svn:http://code.taobao.org/svn/demo_</developerConnection>
<tag>HEAD</tag>
<url>http://code.taobao.org/svn/demo_</url>
</scm>
<build>
.
.
.
</build>
28、weblogic启动报error in opening zip file
从打包好的war文件打开报错的jar会发现打不开,打开其他jar则正常,替换有问题的jar文件为可以打开的jar即可
还有可能是lib下面没有这个包,然后打成war包时是根据pom来打得,就自己创建了一个无法识别的jar文件,所以部署就打不开。这里我的原因是把maven-war-plugin
的filtering
设置成了true,设置为false后jar包打开正常!
5、最后
升级过程中遇到了很多问题,百度了很多文章但是始终不太有借鉴价值。自己在升级的过程中总结了很多经验,希望自己这篇博客可以给正纠结和迷茫的人一些方向,同时也给自己敲一下警钟!