Maven项目版本号

  • 默认版本号: 1.0-SNAPSHOT 最佳实践是约定该版本为不稳定版本,如果发布一定要删除;
  • 建议的版本规则: 主版本号.次版本号.增量版本号-<里程碑版本> 如:1.0.0-RELEASE 10.2.5-FINAL 等. 最佳实践是结合自身情况制定大家都认可的版本号规则.

常见命令

内置的maven插件提供了常见的命令, 可以在以下位置找到对应的包: .m2\repository\org\apache\maven\plugins

  • compile
  • clean 删除/target,将已编译的二进制文件等删除
  • test test case junit/testNG
  • package 打包
  • install 把项目install到本地仓库
  • deploy 发布jar到remote服务器
  • mvn help:system 查看环境变量

插件

插件仓库

常见插件
  • findbugs 静态代码检查
  • versions 统一升级版本号 统一升级版本 http://www.mojohaus.org/versions-maven-plugin/ 可以查看使用示例, 常用的设置版本的命令为mvn versions: set –DnewVersion=1.1.0-final
  • mvn versions:set -DnewVersion=1.1
  • source 打包源代码,当jar提供给外部的时候斟酌使用
  • assembly 打包zip、war
  • tomcat7
  1. <plugins>
  2. <!--静态代码bug扫描-->
  3. <plugin>
  4. <groupId>org.codehaus.mojo</groupId>
  5. <artifactId>findbugs-maven-plugin</artifactId>
  6. <version>3.0.0</version>
  7. <configuration>
  8. <threshold>High</threshold>
  9. <effort>Default</effort>
  10. <findbugsXmlOutput>true</findbugsXmlOutput>
  11. <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
  12. </configuration>
  13. </plugin>
  14. <!--版本号管理-->
  15. <plugin>
  16. <groupId>org.codehaus.mojo</groupId>
  17. <artifactId>versions-maven-plugin</artifactId>
  18. <version>2.3</version>
  19. </plugin>
  20. <!--打包源代码-->
  21. <plugin>
  22. <artifactId>maven-source-plugin</artifactId>
  23. <version>2.3</version>
  24. <executions>
  25. <execution>
  26. <id>attach-sources</id>
  27. <phase>install</phase>
  28. <goals>
  29. <goal>jar-no-fork</goal>
  30. </goals>
  31. </execution>
  32. </executions>
  33. </plugin>
  34. <!--这个有点晕,生成可执行jar包什么的-->
  35. <plugin>
  36. <artifactId>maven-assembly-plugin</artifactId>
  37. <version>3.0.0</version>
  38. <configuration>
  39. <archieve>
  40. <manifest>
  41. <mainClass>com.xlx.Test</mainClass>
  42. </manifest>
  43. </archieve>
  44. <descriptorRefs>
  45. <descriptorRef>jar-with-dependencies</descriptorRef>
  46. </descriptorRefs>
  47. </configuration>
  48. </plugin>
  49. <!--tomcat插件-->
  50. <plugin>
  51. <groupId>org.apache.tomcat.maven</groupId>
  52. <artifactId>tomcat7-maven-plugin</artifactId>
  53. <version>2.2</version>
  54. <configuration>
  55. <port>8080</port>
  56. <path>/</path>
  57. </configuration>
  58. </plugin>
  59. </plugins>

自定义插件

学习地址https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

  1. 创建项目
  2. 修改pom的<packaging>maven-plugin</packaging>
  3. 添加依赖
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.maven</groupId>
  4. <artifactId>maven-plugin-api</artifactId>
  5. <version>LATEST</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.maven.plugin-tools</groupId>
  9. <artifactId>maven-plugin-annotations</artifactId>
  10. <version>LATEST</version>
  11. <scope>provided</scope>
  12. </dependency>
  13. </dependencies>
  1. 写代码实现AbstractMojo
  1. @Mojo(name="xlxTest",defaultPhase = LifecyclePhase.PACKAGE)
  2. public class Test extends AbstractMojo {
  3. /**
  4. * 接收的参数
  5. */
  6. @Parameter
  7. private String message;
  8. /**
  9. * 接收多个值的参数
  10. */
  11. @Parameter
  12. private List<String> options;
  13. /**
  14. * 命令行中接收,注意必须有property mvn:package -Dargs=this is from cmd
  15. */
  16. @Parameter(property = "args")
  17. private String args;
  18. public void execute() throws MojoExecutionException, MojoFailureException {
  19. System.out.println("my first maven plugin message is : " + message);
  20. System.out.println("my first maven plugin options is : " + options);
  21. System.out.println("my first maven plugin args from evm is : " + args);
  22. }
  23. }
  1. mvn install
  2. 使用, maven可以接收参数, 也可以使用环境变量取,如${settings.localRepository},${project.baseUri}等
  1. <!--项目pom修改-->
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <groupId>com.xlx</groupId>
  6. <artifactId>engineering</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <executions>
  9. <execution>
  10. <phase>package</phase>
  11. <goals>
  12. <goal>xlxTest</goal>
  13. </goals>
  14. </execution>
  15. </executions>
  16. <configuration>
  17. <message>message</message>
  18. <options>
  19. <option>one</option>
  20. <option>two</option>
  21. </options>
  22. </configuration>
  23. </plugin>
  24. </plugins>
  25. </build>

Profile

  • 不同运行环境 dev/prod/test等
  • mvn clean package –P dev
  • settings.xml 可以指定不同服务器仓储配置 <profile.active>私服或者官方</profile.active>

多环境配置的配置文件路径

  1. <profiles>
  2. <profile>
  3. <id>dev</id>
  4. <properties>
  5. <profile.active>dev</profile.active>
  6. </properties>
  7. <activation>
  8. <activeByDefault>true</activeByDefault>
  9. </activation>
  10. </profile>
  11. <profile>
  12. <id>test</id>
  13. <properties>
  14. <profile.active>test</profile.active>
  15. </properties>
  16. </profile>
  17. </profiles>
  18. <build>
  19. <resources>
  20. <resource>
  21. <directory>${baseDir}/src/main/resources</directory>
  22. <excludes>
  23. <exclude>conf/**</exclude>
  24. </excludes>
  25. </resource>
  26. <resource>
  27. <directory>src/main/resources/conf/${profile.active}</directory>
  28. </resource>
  29. </resources>
  30. </build>

私服

pom中增加发布节点

  1. <distributionManagement>
  2. <repository>
  3. <id>nexus-release</id>
  4. <name>nexus-release</name>
  5. <url>http://localhost:8099/repository/maven-releases/</url>
  6. </repository>
  7. <snapshotRepository>
  8. <id>nexus-snapshot</id>
  9. <name>nexus-snapshot</name>
  10. <url>http://localhost:8099/repository/maven-snapshots/</url>
  11. </snapshotRepository>
  12. </distributionManagement>

修改settings.xml 增加服务器账号密码信息

  1. <server>
  2. <id>nexus-release</id>
  3. <username>admin</username>
  4. <password>admin123</password>
  5. </server>
  6. <server>
  7. <id>nexus-snapshot</id>
  8. <username>admin</username>
  9. <password>admin123</password>
  10. </server>

生成脚手架

  • mvn archetype: create-from-project 从项目生成脚手架
  • cd /target/generated-soource/archetype 转到此目录
  • mvn install 发布到仓库
  • 可以添加到ide的脚手架列表
  • mvn archetype:generate –DarchetypeCatagory=local 命令行方式创建项目 local参数指定走本地仓库

(二)Java工程化--Maven实践的更多相关文章

  1. (一)Java工程化--Maven基础

    Maven 读作['mevən] 翻译成中文是"内行,专家" Maven是什么 包依赖的前世今生: 原始的jar包引用--> ant --> maven. 是一种项目管 ...

  2. JEECG(二) JEECG框架下调用webservice java springmvc maven 调用 webservice

    JEECG系列教程二 如何在JEECG框架下使用webservice 本文所使用的webservice是c#开发的 其实无论是什么语言开发的webservice用法都一样 java springmvc ...

  3. Java Servlet DAO实践(二)

    Java Servlet DAO实践(二) DAO连接类 package com.seller.servlets.dao; import java.sql.*; public class DataBa ...

  4. Java 理论与实践: 处理 InterruptedException

    捕捉到它,然后怎么处理它? 很多 Java™ 语言方法,例如 Thread.sleep() 和 Object.wait(),都可以抛出InterruptedException.您不能忽略这个异常,因为 ...

  5. Google Developing for Android 二 - Memory 最佳实践 // lightSky‘Blog

    Google Developing for Android 二 - Memory 最佳实践   |   分类于 Android最佳实践 原文:Developing for Android, II Th ...

  6. 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...

  7. Java 理论与实践: 处理 InterruptedException(转)

    很多 Java™ 语言方法,例如 Thread.sleep() 和 Object.wait(),都可以抛出InterruptedException.您不能忽略这个异常,因为它是一个检查异常(check ...

  8. Maven(二)之Maven项目构建演练

    从上一篇的讲解中我们知道了什么是Maven,然后它的安装配置,到修改本地仓库,这篇我们用一个实际的例子,带领大家走进我们的Maven之旅.让我们一起来体验一下Maven的高度自动化构建项目的过程. 一 ...

  9. kafka原理和实践(二)spring-kafka简单实践

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

随机推荐

  1. (十二)Deleting Documents

    Deleting a document is fairly straightforward. This example shows how to delete our previous custome ...

  2. (七)Create an Index

    Now let’s create an index named "customer" and then list all the indexes again: 现在让我们创建一个名 ...

  3. (转)lwip TCP client & FreeRTOS 打开TCP 的 保活机制 LWIP_TCP_KEEPALIVE==1

    参考大神教程:http://blog.sina.com.cn/s/blog_62a85b950101aw8x.html   老衲五木 :http://blog.sina.com.cn/s/blog_6 ...

  4. 极光推送JAVA代码示例

    一. 准备工作 1. 登录极光推送官网https://www.jpush.cn/,注册账号并登录 2. 创建应用 创建应用过程,详见百度经验:http://jingyan.baidu.com/arti ...

  5. go语言-值类型与引用类型

    https://www.cnblogs.com/java-zhao/p/9942311.html https://blog.csdn.net/TDCQZD/article/details/826836 ...

  6. mybatis的where和if标签配合使用

    where标签用于简化sql的书写,if标签用于判断.大概的使用如下 <select id="getCountByPageInfo" parameterType=" ...

  7. xadmin在Django 1.11中的使用及中英文切换

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com xadmin是一个强大的替代django admin的管理后台,github地址为:https://g ...

  8. Python之路1-变量、数据类型、循环语法

    1.python语言介绍 编程语言主要从以下几个角度进行分类,编译型和解释型,静态语言和动态语言,强类型定义语言和弱类型定义语言. 编译和解释区别 编译器是把源程序的每一条语句都编译成机器语言,并保存 ...

  9. [模板] 杜教筛 && bzoj3944-Sum

    杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...

  10. 仙人掌&圆方树学习笔记

    仙人掌&圆方树学习笔记 1.仙人掌 圆方树用来干啥? --处理仙人掌的问题. 仙人掌是啥? (图片来自于\(BZOJ1023\)) --也就是任意一条边只会出现在一个环里面. 当然,如果你的图 ...