maven笔记

手动下载maven依赖(without pom):

使用dependency:get。

如:

mvn dependency:get -DrepoUrl=http://10.2.2.10:8081/nexus/content/repositories/releases -DgroupId=org.apache.httpcomponents -DartifactId=httpclient -Dversion=4.2.1

http://stackoverflow.com/questions/884808/utility-for-downloading-artifacts-from-maven-repo-without-mvn-poms

http://stackoverflow.com/questions/1776496/a-simple-command-line-to-download-a-remote-maven2-artifact-to-the-local-reposito/1776808#1776808

http://stackoverflow.com/questions/3601570/how-to-download-jars-from-maven-central-without-writing-any-pom-xml

 

 

maven命令:

mvn clean compile

mvn clean test

mvn clean package // 打包到target目录

mvn clean install // 安装到本地库

使用Archetype生成项目骨架:

mvn archetype:generate // maven3直接运行这样的命令也只会解析最新的稳定版本,而maven2则下载最新的插件版本

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-5:generate // 如果是 Maven 2,最好运行这样的命令

// 我  们  实  际  上  是  在  运  行  插  件   maven-archetype-plugin  ,  注  意  冒  号  的  分  隔  ,  其  格  式  为groupId:artifactId:version:goal

 

指定编译JDK版本:

1.   <project>     

2.   …     

3.     <build>     

4.       <plugins>     

5.          <plugin>     

6.            <groupId>org.apache.maven.plugins</groupId>     

7.            <artifactId>maven-compiler-plugin</artifactId>     

8.            <configuration>     

9.              <source>1.5</source>     

10.             <target>1.5</target>     

11.           </configuration>     

12.         </plugin>     

13.      </plugins>     

14.    </build>     

15.  …     

16.  </project>

 

可利用finalName自定义打包后的文件名称

 

默认打包生成的 jar 是不能够直接运行的,因为带有 main 方法的类信息不会添加到 manifest 中(我们可以打开 jar 文件中的META-INF/MANIFEST.MF  文件,将无法看到  Main-Class  一行)。为了生成可执行的  jar  文件,我们需要借助maven-shade-plugin,配置该插件如下:

Java 代码       

1. <plugin> 

2.   <groupId>org.apache.maven.plugins</groupId>     

3.     <artifactId>maven-shade-plugin</artifactId>     

4.     <version>1.2.1</version>     

5.     <executions>     

6.       <execution>     

7.         <phase>package</phase>     

8.         <goals>     

9.           <goal>shade</goal>     

10.        </goals>     

11.        <configuration>     

12.          <transformers>     

13.            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

            <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>     

14.           </transformer>     

15.         </transformers>     

16.       </configuration>     

17.       </execution>     

18.    </executions>     

19.  </plugin>   

plugin  元 素 在  POM  中 的 相 对 位 置 应 该 在 <project><build><plugins> 下 面 。 我 们 配 置 了  mainClass  为com.juvenxu.mvnbook.helloworld.HelloWorld,项目在打包时会将该信息放到 MANIFEST 中。现在执行  mvn clean install,待 构 建 完 成 之 后 打 开   target/ 目 录 , 我 们 可 以 看 到   hello-world-1.0-SNAPSHOT.jar和original-hello-world-1.0-SNAPSHOT.jar,前者是带有  Main-Class  信息的可运行  jar,后者是原始的  jar,打开hello-world-1.0-SNAPSHOT.jar 的 META-INF/MANIFEST.MF,可以看到它包含这样一行信息: 

Main-Class: com.juvenxu.mvnbook.helloworld.HelloWorld 

现在,我们在项目根目录中执行该 jar 文件: 

D: \code\hello-world>java -jar target\hello-world-1.0-SNAPSHOT.jar 

Hello Maven 

控制台输出为 Hello Maven,这正是我们所期望的。 

 

maven坐标:

groupId: // 必须定义

artifactId: // 必须定义

version: // 必须定义

packaging:[jar|war|ear|ejb|pom|...] // default:jar;可选

classifier: // 定义构建输出的附属构件,不能直接在项目定义

 

项目构件文件名:

一般规则:artifactId-version[-classifier].packaging

 

依赖范围(scope):

compile:

test:

provided: // 对于编译和测试classpath有效,但在运行时无效,如servlet-api。

runtime: // 对于测试和运行classpath有效,但在编译主代码时无效,如JDBC驱动。

system: // 系统依赖范围,即本地依赖。与三种classpath的关系,和provided完全一致。必须通过systemPath元素显式指定依赖文件的路径。

import: // 不会对三种classpath产生实际的影响。

 

 

 依赖范围影响传递性依赖

 

compile

test

provided

runtime

compile

compile

-

-

compile

test

test

-

-

test

provided

provided

-

provided

provided

runtime

runtime

-

-

runtime

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值