zoukankan      html  css  js  c++  java
  • [Maven]初次实践

    都说Maven好,以前一直用ant,这次体验一下.

    开始之前,maven给我的印象有2个,一是库依赖管理做得比较好,二是规范了构建编译过程,说白了就是什么目录都规定好了.

    好开始安装,解压缩,设置m2_home, mave_opts,path环境变量,在命令行执行"mvn help:describe -Dplugin=help"...

    [WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5: ...

    ...

    [ERROR] No plugin found for prefix 'help' in the current project and in the plugin groups [org.apache.maven.plugins, org
    .codehaus.mojo] available from the repositories [local (D: mpmvn-test), central (https://repo.maven.apache.org/maven2)
    ] -> [Help 1]

    咦,出错了, 根据错误信息执行"mvn help:describe -Dplugin=help -e -X"

    Caused by: org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact org.apache.maven.plugins:m
    aven-install-plugin:pom:2.4 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443
     [repo.maven.apache.org/185.31.17.215] failed: Connection timed out: connect

    网络连接有问题,看来应该设置代理,直接修改conf/setting.xml, 把代理加上去.

    运行,咦,又tm出错了, 错误信息是(注意黑斜体):

    [WARNING] Failure to transfer org.codehaus.mojo/maven-metadata.xml from https://repo.maven.apache.org/maven2 was cached
    in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates
    are forced. Original error: Could not transfer metadata org.codehaus.mojo/maven-metadata.xml from/to central (https://re
    po.maven.apache.org/maven2): Not authorized by proxy , ReasonPhrase:Proxy Authentication Required.
    org.eclipse.aether.transfer.MetadataTransferException: Failure to transfer org.codehaus.mojo/maven-metadata.xml from htt
    ps://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the updat
    e interval of central has elapsed or updates are forced. Original error: Could not transfer metadata org.codehaus.mojo/m
    aven-metadata.xml from/to central (https://repo.maven.apache.org/maven2): Not authorized by proxy , ReasonPhrase:Proxy A
    uthentication Required.

    这如何是好,明明不需要密码的, baidu一把, oh,原来有人碰到类似情况, 如果是NTLM proxy(搞不清楚这个代理类型,可能是微软的东东吧),需要添加一个附加包,如下:

    Download the wagon-http-lightweight-2.2.jar , copy到libext目录下, 问题解决!

    接下来看看帮助信息(很重要):

    mvn help:help -Ddetail=true         <-查看help

    mvn help:describe -Dplugin=help  <-查看help插件

    mvn help:describe -Dplugin=help -Dgoal=system -Ddetail <-查看help插件下的system goal详细信息。

    mvn help:describe -Dcmd=jboss-as:deploy -Ddetail=true <-一个例子, 文档中说要执行 mvn jboss-as:deploy, 这个命令将查看 jboss-as:deploy的帮助

    好,写了一个HelloWorld.java放到srcmainjava中,注意目录结构

    src
    srcmainjava
    pom.xml
    

    pom.xml

    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>bjfarmer</groupId>
        <artifactId>MyTest</artifactId>
        <version>1.0</version>
    </project>

    不知道为何modelVersion必须是4.0.0,否则报错,这个留在后面找答案,运行"mvn clean install",继续。

    这时候mvn又开始下载插件(resource,compiler,surefire...),额地神,到处都是插件,看来离开了插件不能活啊。可恶的代理,时断时续,我不得不重复执行clean install.总算可以了。这时候发现build后发现多了一个target目录,如下:

    src 
    srcmainjava 
    	arget
    	argetclasses    <-编译的class
    	argetmaven-archiver <-打包的参数设置
    	argetmaven-status <-状态,里面只有一个文件夹
    	argetmaven-statusmaven-compiler-plugin  <-里面记录了要编译的源文件清单,目标类文件清单等
    	argetMyTest-1.0.jar  <-最终生成的jar包
    pom.xml

     那么现在问题来了,这个编译到底用了哪个版本的JDK呢?这样的黑盒也太黑了,不行我得搞清楚。

    不看不知道,一看吓一跳,将包MyTest-1.0.jar解开,然后用16进制查看5~6(minor version),7~8字节(major version), 十进制为49.0, 对应于JRE为1.5。注:如果您还不知道如何查看一个class是从那个版本JDK编译来的,请阅读JVM Specification Chapter4,或者用JDK自带工具 javap -verbose <class file>

    查看插件compiler的帮助,看官可能有些疑问,这么高级的命令你怎么一开始就知道?其实不是,就是学习了基本的help命令之后,稍微理解了插件是由很多goal组成,而每个goal都有些parameter。执行下面命令:

    mvn help:describe -Dplugin=compiler -Dgoal=compile -Ddetail=true

    ...

    target (Default: 1.5)
      User property: maven.compiler.target
      The -target argument for the Java compiler.

    ....

    果真,不制定版本,默认真是1.5,这真是领教了“convention over configuration”,执行下面的命令,

    mvn clean install -Dmaven.compiler.target=1.7

    发现class文件major version变成了51,-;

    到现在为止已经可以build一个简单项目了,还缺少Junit test,再努力一把,加入如下的dependency, 把Junit 测试代码放到src estjava下, 重新运行mvn clean install, ok!

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>

    当然用mvn默认的项目创建更好,譬如:

    mvn archetype:create -DgroupId=<group> -DartifactId=<name> -DpackageName=com.zproject

    草,失败了,看了一下帮助(mvn help:describe -Dplugin=archetype -Ddetail=true),这个goal deprecated, 建议用generate

    mvn archetype:generate -DgroupId=<group> -DartifactId=<name> -DpackageName=com.zproject

     终于成了,注意到默认源码目录中的package不是com.zproject, 而是groupId。有些意思。

    好了回顾一下,重新领略一下maven的一些重要的概念,这片算是完结了。

    1.  Convention over Configuration (约定优于配置), 黑盒子,不用自己定义了,方便的同时带来了麻烦。
    2. 依赖管理,确实方便,根据坐标(groupId:artifactId:version:package,如junit:junit:3.8.1:jar)直接寻找,不用自己管理。
    3. 生命周期,预定义了软件生命周期,resources, compile, testResources,testCompile, test, jar, 执行这些生命周期等同于相应的任务组合。
    4. 插件,插件,插件由goal组成,譬如 mvn compiler:compile,这个就是执行一个插件的goal。
    5. 还有2个重要命令: mvn dependency:tree  mvn dependency:resolve
    6. Profile, 上文中没有提到,啥东西? 先记下这个命令: mvn help:active-profiles   mvn clean install -P<profile-name> 构建移植,嗯,profile就是干这个的. 譬如开发环境的部署与生产环境的部署.

    over,有机会写一篇高级技巧。

  • 相关阅读:
    redis
    一句话总结面向对象
    HTML鼠标悬停改变样式
    div 在css中透明度怎么调?
    SpringMyBatisDay03
    list与Set、Map区别及适用场景
    Java之构造器和构造方法的使用和意义
    Set keys=Map.keyset()
    SpringMyBatisDay02
    CSS选择器可以用数字开头吗
  • 原文地址:https://www.cnblogs.com/bjfarmer/p/5197180.html
Copyright © 2011-2022 走看看