(1)简单mavn工程创建及操作 1、创建简单的工程 mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook 2、执行mvn package命令执行过程如下:(生命周期,在mavn进行打包过程中,执行的顺序如下) resources:resources \ compiler:compile \ resources:testResources \ compiler:testCompile \ surefire:test \ jar:jar 3、maven坐标信息(在pom.xml中的信息) <groupId>org.sonatype.mavenbook.ch03</groupId> <artifactId>simple</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> 4、转eclipse工程 (1):简单转换 mvn eclipse:eclipse (2):会将工程将框架转换为eclipse工程,并从远程下载jar包到本地仓库(window下是(C:\Documents and Settings\${username}\.m2\repository) mvn eclipse:eclipse -DdownloadSources=true (3):path-to-eclipse-workspace是本机的eclipse的worksapce的路径。执行后maven会在eclipse中建立一个M2_REPO环境变量,并将其中所有的jar包引入到工程中,完全自动化 mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo 5、运行main函数类 mvn install mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main 6、查看插件用法的命令 mvn help:describe -Dplugin=exec -Dfull 7、查看依赖的类 mvn dependency:resolve 8、查看整个项目依赖的类树结构 mvn dependency:tree 9、查看完整的依赖踪迹 mvn install -X 10、自动化测试程序 mvn test 11、通过命令行的方式忽略测试失败结果信息 mvn test -Dmaven.test.failture.ignore=true 12、跳过单元测试命令 mvn install -Dmaven.test.skip=true 13、Maven Assembly 插件的预定义装配描述符生成一个可分发的 JAR 文件的过程,该文件包含了项目的二进制文件和所有的依赖。 执行命令:mvn install assembly:assembly 14、包含所有依赖的jar运行方式如下: java -cp simple-weather-jar-width-dependency.jar org.sonatype.mavenbook.weather.Main 问题:当加入新的依赖包时,可执行mvn install下载包,但在eclipse中需要手动加入包才能正常操作信息。 (2)简单web应用工程创建和操作 1、创建web工程的命令如下: mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch05 -DartifactId=simple-webapp -DpackageName=org.sonatype.mavenbook.web -DarchetypeArtifactId=mavn-archetype-webapp 2、配置Maven Jetty 插件,在 Maven 中运行你的 web应用 mvn jetty:run 3、清理重新打包操作 mvn clean install (3)多个模块工程的创建和操作 注意问题 (1)父工程和子工程之间的关联关系要清楚 如果父工程和子工程在同一目录下,配置路径一定要正确。同时,子工程要添加<relativePath>节点指向父工程的pom.xml文件。 父工程依赖的jar包或插件,在子工程中无需配置,因为子工程已继承父工程的配置,包括groupId、version等信息。 结构图一 simple-parent——————————simple—weather | ———————simple-webapp 配置信息如下所示: 1、simple-parent <modules> <module>simple-weather</module> <module>simple-webapp</module> </modules> 2、simple—weather <!-- 子模块中无需定义groupId和version信息,这些信息都从父模块中继承 --> <parent> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple-parent</artifactId> <version>1.0</version> </parent> <artifactId>simple-weather</artifactId> <packaging>jar</packaging> <name>Simple-Weather</name> 3、simple-webapp <!-- 子模块中无需定义groupId和version信息,这些信息可以从父模块中继承 --> <parent> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple-parent</artifactId> <version>1.0</version> </parent> <artifactId>simple-webapp</artifactId> <packaging>war</packaging> <name>simple-webapp</name> <url>http://maven.apache.org</url> 由于simple-webapp依赖于simple—weather,此时需要在<dependences>节点中添加工程依赖 <!-- 该工程依赖weather工程,需要将weather工程的依赖添加进来--> <dependency> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple-weather</artifactId> <version>1.0</version> </dependency> 结构图二 |——simple-parent |——simple—weather |——simple-webapp 配置信息如下所示: 1、simple-parent <modules> <module>../simple-weather</module> <module>../simple-webapp</module> </modules> 2、simple—weather <!-- 子模块中无需定义groupId和version信息,这些信息都从父模块中继承 --> <parent> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple-parent</artifactId> <version>1.0</version> <!-- 如果没有该属性则该工程必须放到simpe-parent下 --> <relativePath>../simple-parent/pom.xml</relativePath> </parent> <artifactId>simple-weather</artifactId> <packaging>jar</packaging> <name>Simple-Weather</name> 3、simple-webapp <!-- 子模块中无需定义groupId和version信息,这些信息可以从父模块中继承 --> <parent> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple-parent</artifactId> <version>1.0</version> <!-- 如果没有该属性则该工程必须放到simpe-parent下 --> <relativePath>../simple-parent/pom.xml</relativePath> </parent> <artifactId>simple-webapp</artifactId> <packaging>war</packaging> <name>simple-webapp</name> <url>http://maven.apache.org</url> 由于simple-webapp依赖于simple—weather,此时需要在<dependences>节点中添加工程依赖 <!-- 该工程依赖weather工程,需要将weather工程的依赖添加进来--> <dependency> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple-weather</artifactId> <version>1.0</version> </dependency> (4)多模块工程的优化原则 1、将各个工程中用到的重复jar包提取到parent的pom.xml中,通过properties属性标注版本信息,子模块只记录jar包,而不记录版本号 父模块定义如下: <properties> <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version> <hsqldb.version>1.8.0.7</hsqldb.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>${hibernate.annotations.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>${hibernate.annotations.version}</version> </dependency> <dependencies> <dependencyManagement> 子模块中定义如下: <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>simple-model</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jta_1.1_spec</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </dependency> </dependencies> 2、子模块都应用父模块的grouId和version, <parent> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple-parent</artifactId> <version>1.0</version> <!-- 如果没有该属性则该工程必须放到simpe-parent下--> <relativePath>../simple-parent/pom.xml</relativePath> </parent> <artifactId>simple-persist</artifactId> <packaging>jar</packaging> 同时,该模块依赖其他模块时采用如下方式配置: <dependency> <groupId>${project.groupId}</groupId> <artifactId>simple-model</artifactId> <version>${project.version}</version> </dependency> 3、采用mvn dependency:analyze将每个工程中的间接依赖添加到每个模块的工程中。
Maven权威指南学习笔记(1-8章)
猜你喜欢
转载自gaojiewyh.iteye.com/blog/793489
今日推荐
周排行