Maven项目搭建及常用包添加
Maven用的是Eclipse插件,插件地址:http://m2eclipse.sonatype.org/sites/m2e
安装以后,就可以用它来建立项目了。
一、首先新建一个 Maven project,在Next中选择Create a simple project 这样就新建一个Maven项了。
二、修改.project文件(可以在Navigator视图中看到)
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
三、修改工程的Project Facets,选中项目右键点击Properties,在Project Facets中选择Dynamic Web Module 和 Jave(只有经过上述两步操作才能在Tomcat中发布),如果修改此处后还是无法在Apace的tomcat service中Add此项目,就可以查看org.eclipse.wst.common.project.facet.core.xml的文件,里面配置有各种版本信息。
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.6"/>
<installed facet="jst.web" version="2.5"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
我在搭建的时候<installed facet="jst.web" version="3.0"/>太高,修改后才能Add。
四、配置输出,在 Properties 中的Deployent Assembly 中配置项目Publish时,项目中的文件对应Publish后路径。记得加上Maven包,只有这样Maven管理的包才能发布过去。或者可以直接修改修改.settings文件夹下的org.eclipse.wst.common.component文件中的配置,此配置是配置发布后对应的路径。如:
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="xiao">
<wb-resource deploy-path="/" source-path="/src/main/webapp"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>
<wb-resource deploy-path="/WEB-INF/lib" source-path="/src/main/webapp/WEB-INF/lib"/>
<property name="context-root" value="xiao"/>
<property name="java-output-path" value="/xiao/target/classes"/>
</wb-module>
</project-modules>
五、在Properties for Maven 下的Java Build Path 的Order and Export中配置 Build Path,勾上Maven的管理包。或者修改.classpath文件,在其中加入包的依赖,这样在发布时可以将由Maven管理的包直接发布过去。如:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Dependencies:添加jar包的页面,很重要!
Plugins:添加maven插件的页面.比如tomcat-maven-plugin等.
Reporting:从没用过,无视~
Dependency Hierarchy:用于显示jar包的依赖关系.没事的时候可以看看jar包的依赖关系.
Effective POM:显示maven的编译路径,plugin之类的.也可以无视.
pom.xml:导入jar包的信息,可以在其中进行修改.重要
<id>jboss-public-repository</id>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>jboss-deprecated-repository</id>
<repositories>
<repository>
<id>jboss-deprecated-repository</id>
<name>JBoss Deprecated Maven Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/deprecated/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>