关于Maven Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。
本文记录Maven开发过程中常用的脚本和遇到的问题。
Maven Dependency 在POM 4中,中还引入了,它主要管理依赖的部署。目前可以使用5个值:
* compile,缺省值,适用于所有阶段,会随着项目一起发布。
* provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
* runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
* test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
* system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
Maven常用指令 package
:打包war:exploded
:编译不生成war包install
:安装到本地资源库 eg:mvn install:install-file -Dfile=lt.util-1.0.jar -DgroupId=com.lt -DartifactId=util -Dversion=1.0 -Dpackaging=jar
process-resources
:编译并打包资源 新建项目:mvn archetype:generate -DgroupId=com.lt -DartifactId=uadb.etl -DarchetypeArtifactIdmaven-archetype-webapp -DinteractiveMode=false
maven dependency exclusion maven远程库
当然,在国内还是老实参考开源中国社区的教程 配置maven
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <mirrors > true<mirror > truetrue<id > nexus-osc</id > truetrue<mirrorOf > central</mirrorOf > truetrue<name > Nexus osc</name > truetrue<url > http://maven.oschina.net/content/groups/public/</url > true</mirror > true<mirror > truetrue<id > nexus-osc-thirdparty</id > truetrue<mirrorOf > thirdparty</mirrorOf > truetrue<name > Nexus osc thirdparty</name > truetrue<url > http://maven.oschina.net/content/repositories/thirdparty/</url > true</mirror > </mirrors >
maven依赖离线下载-1maven
上传项目到Maven Central Repository Maven本地开发
下载apache-maven
设置本地库路径 在maven\config\settings.xml中设置本地库路径:<localRepository>D:\Dev\Maven-Respository</localRepository>
在Myeclipse中设置安装路径 在Window>Preferences>Myeclipse>Maven4Myeclipse>Installations
中执行Add加入本地maven路径 在Window>Preferences>Myeclipse>Maven4Myeclipse>User Settings
中Browser选择maven\config\settings.xml,执行Update Settings,Reindex
maven引用本地jar包 假设将包htmlparser.jar放入了项目下的lib目录中 -> ${project}/lib/htmlparser.jar,则pom.xml中应该配置如下:
1 2 3 4 5 6 7 <dependency > <groupId > com.htmlparser</groupId > <artifactId > htmlparser</artifactId > <version > 2.0</version > <scope > system</scope > <systemPath > ${project.basedir}/lib/htmlparser.jar</systemPath > </dependency >
Maven项目聚合 为解决多个依赖项目自动打包,可通过聚合maven项目解决 pom示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > true<modelVersion > 4.0.0</modelVersion > true<groupId > com.lt.util</groupId > true<artifactId > util.aggregation</artifactId > true<version > 0.0.1</version > true<packaging > pom</packaging > true<name > util.aggregation</name > true<properties > truetrue<project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > true</properties > true<modules > truetrue<module > ../util.common</module > truetrue<module > ../util.jdbc</module > truetrue<module > ../util.web</module > truetrue<module > ../util.geo</module > truetrue<module > ../util.hibernate</module > true</modules > </project >
通过
mvn clean install
进行打包
Maven项目依赖继承 uadb.parent
项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > true<modelVersion > 4.0.0</modelVersion > true<groupId > com.lt.uadb</groupId > true<artifactId > uadb.parent</artifactId > true<version > 0.0.1</version > true<packaging > pom</packaging > true<name > uadb.parent</name > true true<properties > truetrue truetrue<jdk.version > 1.7</jdk.version > truetrue truetrue<junit.version > 4.8.2</junit.version > truetrue truetrue<project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > true</properties > true<dependencyManagement > truetrue<dependencies > truetruetrue truetruetrue<dependency > truetruetruetrue<groupId > junit</groupId > truetruetruetrue<artifactId > junit</artifactId > truetruetruetrue<version > ${junit.version}</version > truetruetruetrue<scope > test</scope > truetruetrue</dependency > truetrue</dependencies > true</dependencyManagement > </project >
child项目引用parent项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > true<modelVersion > 4.0.0</modelVersion > true<artifactId > uadb.etl.pre</artifactId > true<version > 1.0</version > true<name > uadb.etl.pre</name > true<packaging > jar</packaging > true<parent > truetrue<groupId > com.lt.uadb</groupId > truetrue<artifactId > uadb.parent</artifactId > truetrue<version > 0.0.1</version > true</parent > true true<properties > truetrue truetrue<jdk.version > 1.7</jdk.version > truetrue truetrue<junit.version > 4.8.2</junit.version > truetrue truetrue<project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > true</properties > true<dependencies > truetruetrue <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > </dependency > true</dependencies > </project >
复制依赖项 dependency:copy-dependencies 输出到target/deps
在Github搭建个人Maven仓库 deploy到本地目录 本地目录提交到gtihub上 配置github地址为仓库地址 Maven问题记录
本地库有改jar包但是依旧无法编译 本地下载的Zip已损坏,删除本地库中的jar文件和目录,重新从远处库下载
远程库只能下载索引,不能下载jar! 日志:The container ‘Maven Dependencies’ references non existing library ‘D:\soft\Maven\Maven-Respository\org\apache\ant\ant\1.9.3\ant-1.9.3.jar’ 解决: So I get you are using Eclipse with the M2E plugin. Try to update your Maven configuration : In the Project Explorer, right-click on the project, Maven -> Update project. If the problem still remains, try to clean your project: right-click on your pom.xml, Run as -> Maven build (the second one). Enter “clean package” in the Goals fields. Check the Skip Tests box. Click on the Run button.
cannot find maven installation embedded Simply remove the external maven installation. When you restart eclipse, the embedded maven will reappear.
install offline问题 The repository system is offline but the artifact org.apache.maven.plugins:maven-install-plugin:pom:2.3.1 is not available in the local repository. 取消勾选offline选项,重新执行install
未找到相关的 Issues 进行评论
请联系 @geosmart 初始化创建