首页 > 学院 > 开发设计 > 正文

Maven笔记

2019-11-06 09:21:14
字体:
来源:转载
供稿:网友

Maven(http://maven.apache.org/)是一款软件项目管理工具。 1. 默认情况下,java源码应该放在src/main/java/……目录下,测试源码应该放在src/main/test/……目录下 2. mvn clean compile生成.class文件,mvn clean install生成.class文件和.jar文件 3. 当仓库里不存在pom文件中的依赖时,第一次会报这样的错误: [ERROR] Failed to execute goal on PRoject hello: Could not resolve dependencies for project com.cisco.mytest:hello:jar:1.0.0-SNAPSHOT: Could not find artifact junit:junit:jar:4.13 in central (https://repo.maven.apache.org/maven2) -> [Help 1] 然后在~/.m2/repository本地仓库中会有两个文件:***.jar.lastUpdated***.pom.lastUpdated 当再次运行mvn时,会报这样的错误: [ERROR] Failed to execute goal on project hello: Could not resolve dependencies for project com.cisco.mytest:hello:jar:1.0.0-SNAPSHOT: Failure to find junit:junit:jar:4.13 in 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 -> [Help 1] 4. mvn clean compile(会生成.class文件) mvn clean test(会执行src目录下的test文件夹里的内容,执行之前会执行compile) mvn clean package(会生成.jar文件,在执行之前会执行test) mvn clean install(会将.jar文件安装到~/.m2/repository,在执行之前会执行package) 5.如果在java源码中加入包信息语句package com.cisco.mytest.hello;,那么在target目录下就会生成带有相同目录结构的文件夹com/cisco/mytest/hello和class文件,如果不加包信息,则不生成上述文件夹,只有class文件 6.在java源码中加入包信息语句package com.cisco.mytest.hello;,那么我在class目录下运行java HelloWorld会提示错误: 找不到或无法加载主类 HelloWorld。应该在src/main/java目录下运行java com.cisco.mytest.hello.HelloWorld,在合适的目录下,运行带有包信息的命令 7. 仓库路径与坐标的关系为:groupId/artifactId/version/artifactId-version.packaging,例如依赖org.sonatype.nexus:nexus-indexer:2.0.0,其对应的路径为org/sonatype/nexus/nexus-indexer/2.0.0/nexus-indexer-2.0.0.jar,jar包文件名的格式为artifactId-version.packaging,所以artifactId应该使用实际项目名作为前缀,比如nexus-indexer 8. 聚合模块:聚合模块是顶层模块,在其POM文件中,<packaging>应该为pom(默认为jar),无<dependency>,子模块写在<module>中,<module>中的子模块名字必须与其目录文件名一致,所以子模块目录文件名应该与artifactId一致,这样<module>就与artifactId一致了 9. 继承模块:其POM文件中的<packaging>也为pom、 10. 一般都将继承模块和聚合模块放在一起融合使用

【Reference】 《Maven实战》,许晓斌,机械工业出版社 http://download.csdn.net/detail/u011388550/9765023


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表