java工程化_(二)Java工程化--Maven实践(示例代码)
Maven項目版本號
默認版本號: 1.0-SNAPSHOT 最佳實踐是約定該版本為不穩定版本,如果發布一定要刪除;
建議的版本規則: 主版本號.次版本號.增量版本號- 如:1.0.0-RELEASE 10.2.5-FINAL 等. 最佳實踐是結合自身情況制定大家都認可的版本號規則.
常見命令
內置的maven插件提供了常見的命令, 可以在以下位置找到對應的包: .m2epositoryorgapachemavenplugins
compile
clean 刪除/target,將已編譯的二進制文件等刪除
test test case junit/testNG
package 打包
install 把項目install到本地倉庫
deploy 發布jar到remote服務器
mvn help:system 查看環境變量
插件
插件倉庫
https://maven.apache.org/plugins/
http://www.mojohaus.org/plugins.html
常見插件
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
org.codehaus.mojo
findbugs-maven-plugin
3.0.0
High
Default
true
target/site
org.codehaus.mojo
versions-maven-plugin
2.3
maven-source-plugin
2.3
attach-sources
install
jar-no-fork
maven-assembly-plugin
3.0.0
com.xlx.Test
jar-with-dependencies
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8080
/
自定義插件
創建項目
修改pom的maven-plugin
添加依賴
org.apache.maven
maven-plugin-api
LATEST
org.apache.maven.plugin-tools
maven-plugin-annotations
LATEST
provided
寫代碼實現AbstractMojo
@Mojo(name="xlxTest",defaultPhase = LifecyclePhase.PACKAGE)
public class Test extends AbstractMojo {
/**
* 接收的參數
*/
@Parameter
private String message;
/**
* 接收多個值的參數
*/
@Parameter
private List options;
/**
* 命令行中接收,注意必須有property mvn:package -Dargs=this is from cmd
*/
@Parameter(property = "args")
private String args;
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("my first maven plugin message is : " + message);
System.out.println("my first maven plugin options is : " + options);
System.out.println("my first maven plugin args from evm is : " + args);
}
}
mvn install
使用, maven可以接收參數, 也可以使用環境變量取,如${settings.localRepository},${project.baseUri}等
com.xlx
engineering
1.0-SNAPSHOT
package
xlxTest
message
one
two
Profile
不同運行環境 dev/prod/test等
mvn clean package –P dev
settings.xml 可以指定不同服務器倉儲配置私服或者官方
多環境配置的配置文件路徑
dev
dev
true
test
test
${baseDir}/src/main/resources
conf/**
src/main/resources/conf/${profile.active}
私服
下載: https://www.sonatype.com/download-oss-sonatype?hsCtaTracking=920dd7b5-7ef3-47fe-9600-10fecad8aa32%7Cf59d5f10-099f-4c66-a622-0254373f4a92
安裝, 解壓即可, 如果需要修改端口號等信息, 可以修改文件
exus-3.13.0-01etc
exus-default.properties
啟動命令 轉到
exus-3.13.0-01in下, nexus /run 可查看啟動日志
使用: http://books.sonatype.com/nexus-book/reference3/index.html
pom中增加發布節點
nexus-release
nexus-release
http://localhost:8099/repository/maven-releases/
nexus-snapshot
nexus-snapshot
http://localhost:8099/repository/maven-snapshots/
修改settings.xml 增加服務器賬號密碼信息
nexus-release
admin
admin123
nexus-snapshot
admin
admin123
生成腳手架
mvn archetype: create-from-project 從項目生成腳手架
cd /target/generated-soource/archetype 轉到此目錄
mvn install 發布到倉庫
可以添加到ide的腳手架列表
mvn archetype:generate –DarchetypeCatagory=local 命令行方式創建項目 local參數指定走本地倉庫
總結
以上是生活随笔為你收集整理的java工程化_(二)Java工程化--Maven实践(示例代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如果100级了,用果儿糖刷新会和重一级(
- 下一篇: java语言基础final_java语言