webxml attribute is required问题
mvn install一个web项目时,报错如下:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war
(default-war) on project com.alpha.webapp: Error assembling WAR:
webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
原因:
maven的web项目默认的webroot是在src\main\webapp。如果在此目录下找不到web.xml就抛出以上的异常。
解决:
需要在pom.xml中增加<webResources>配置,如下:
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>WebContent</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
或者增加<webXml>配置,如下:
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
<directory>WebContent</directory>配置为网站根目录,例如本例的目录为:
现在mvn install该项目就能构建成功:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building com.alpha.webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ com.alpha.webapp ---
[INFO] Deleting D:\Users\axpss\workspace\com.alpha.webapp\target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ com.alpha.webapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ com.alpha.webapp ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Users\axpss\workspace\com.alpha.webapp\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ com.alpha.webapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ com.alpha.webapp ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ com.alpha.webapp ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ com.alpha.webapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [com.alpha.webapp] in [D:\Users\axpss\workspace\com.alpha.webapp\target\simple-webapp]
[INFO] Processing war project
[INFO] Copying webapp webResources [D:\Users\axpss\workspace\com.alpha.webapp\WebContent]
to [D:\Users\axpss\workspace\com.alpha.webapp\target\simple-webapp]
[INFO] Copying webapp resources [D:\Users\axpss\workspace\com.alpha.webapp\src\main\webapp]
[INFO] Webapp assembled in [32 msecs]
[INFO] Building war: D:\Users\axpss\workspace\com.alpha.webapp\target\simple-webapp.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ com.alpha.webapp ---
[INFO] Installing D:\Users\axpss\workspace\com.alpha.webapp\target\simple-webapp.war
to D:\dev\maven-repo\com\alpha\com.alpha.webapp\0.0.1-SNAPSHOT\com.alpha.webapp-0.0.1-SNAPSHOT.war
[INFO] Installing D:\Users\axpss\workspace\com.alpha.webapp\pom.xml
to D:\dev\maven-repo\com\alpha\com.alpha.webapp\0.0.1-SNAPSHOT\com.alpha.webapp-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.078s
[INFO] Finished at: Thu Mar 28 15:55:56 CST 2013
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------
将war部署到JBoss
从本地Maven库中找到com.alpha.webapp-0.0.1-SNAPSHOT.war,拷贝到JBoss/server/*/deploy目录。
如果已打开JBoss,因为支持热部署,马上就能在控制台看到如下信息,表明部署成功:
打开网页,看效果:
com.alpha.webapp-0.0.1-SNAPSHOT是war文件名称,hello是web.xml中定义的<url-pattern>