微服务架构—JAVA打包黑科技

为什么80%的码农都做不了架构师?>>>   hot3.png

1 背景

每个JAVA项目开发完成后都会考虑部署至各个环境(DEV、TEST、PRO等)中,选择一种好的打包方式,将会在使用中无形的减少不少工作量,同时也会带来很多方便之处。反之,没有选择好打包方式,则会带来诸多不变之处。 下面我将介绍几种JAVA工厂常见的打包方式,先上效果图:

微服务架构—JAVA打包黑科技_第1张图片微服务架构—JAVA打包黑科技_第2张图片微服务架构—JAVA打包黑科技_第3张图片

2 spring-boot-maven-plugin

spring-boot-maven-plugin是Springboot提供的打包插件,直接在POM中添加一下Plugin即可:


        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    cn.micro.demo.DemoApplication
                
            
        

3 自定义打包脚本

如果你的脚本编写能力很高,那完全可以考虑自己编写一套适合自己的打包脚本出来,不过因为难度和复杂度都较大所以不建议自己编写,等常用的方式解决不了你的需求时再考虑。

4 IDEA手动打包

常用的IDEA开发工具都是可以可视化界面打包部署包的,不过这种方式不利于CI/CD流程,所以也不推荐。

5 JSW+Assembly

JSW+Assembly是JAVA工程MAVEN通用打包方式。精简方式则直接使用:appassembler-maven-plugin和maven-assembly-plugin即可完成打包。但为了更好的效果,我将引入几个好用的插件来配合完成打包。

5.1 maven-compiler-plugin

maven-compiler-plugin插件主要用于指定编译时的JDK版本。



	org.apache.maven.plugins
	maven-compiler-plugin
	3.7.0
	
		1.8
		1.8
	

5.2 maven-jar-plugin

maven-jar-plugin插件主要用于项目打包时,排除配置文件不打包进jar包中(如果配置打入了jar,则每次变更配置,都需要重新打包,很不方便)。



	org.apache.maven.plugins
	maven-jar-plugin
	2.6
	
		
		
			
			*.yml
			*.xml
			*.properties
			static/**
			
			*.conf
			tools/**
		
	
	
		
			package
			
				jar
			
		
	

5.3 maven-enforcer-plugin

maven-enforcer-plugin插件主要用于打包时检测各种规范要求,如:检查JDK版本、MAVEN版本、不能依赖快照等功能。




	org.apache.maven.plugins
	maven-enforcer-plugin
	3.0.0-M2
	
		
			default-cli
 			
				enforce
 			
			validate
			
				
					
						
							
						
						[3.3.3,)
					
					
						
							
						
						[1.8.0,)
					
					
						No Snapshots Allowed!
					
				
			
		
	

5.4 git-commit-id-plugin

git-commit-id-plugin用于将当前GIT项目的Commit日志记录打包成一个文件,便于查看当前物料包的历史GIT日志(如使用场景:线上跑的代码,想看看是否提交了某个BUG的修复记录)。



	pl.project13.maven
	git-commit-id-plugin
	
		
			
				revision
			
		
	
	
		true
		yyyy-MM-dd'T'HH:mm:ssZ
		true
		${project.build.directory}/dist/jsw/app/conf/git.properties
	

5.5 appassembler-maven-plugin

appassembler-maven-plugin用于将当前项目打包成个性化的目录框架,并同时使用JSW(Java Service Wrapper)生成各种操作系统的运维脚本(启动、停止、查看状态、重启等等命令),并且赋予可执行的权限等操作。同时也可以指定JMX端口、远程DEBUG端口、GC配置、GC日志、JVM配置和内存溢出Dump等信息。




	org.codehaus.mojo
	appassembler-maven-plugin
	2.0.0
	
		UTF-8
		bin
		tmp
		logs
		lib
		flat
		${project.build.directory}/dist
		conf
		true
		src/main/resources
		
			
				app
				
				cn.micro.demo.Main
				
					
					jsw
				
				
					
						jsw
						
							aix-ppc-32
							aix-ppc-64
							linux-ppc-64
							linux-x86-32
							linux-x86-64
							windows-x86-32
							windows-x86-64
							hpux-parisc-64
							solaris-x86-32
							solaris-sparc-32
							solaris-sparc-64
							macosx-ppc-32
							macosx-universal-32
							macosx-universal-64
						
						
							
								configuration.directory.in.classpath.first
								conf
							
							
								wrapper.ping.timeout
								120
							
							
								set.default.REPO_DIR
								lib
							
							
								wrapper.logfile
								logs/wrapper.log
							
						
					
				
				
					
					
						java.security.policy=conf/policy.all
						com.sun.management.jmxremote
						com.sun.management.jmxremote.port=8777
						com.sun.management.jmxremote.authenticate=false
						com.sun.management.jmxremote.ssl=false
					
					
					
					
					
						-server
						
						-Xdebug
						-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5010
						
						-XX:+HeapDumpOnOutOfMemoryError
						-XX:HeapDumpPath=logs/heap-dump.hprof
						
						-XX:+UseG1GC
						-XX:MaxGCPauseMillis=200
						-XX:InitiatingHeapOccupancyPercent=45
						-XX:G1ReservePercent=10
						-XX:NewRatio=2
						-XX:SurvivorRatio=8
						-XX:MaxTenuringThreshold=15
						
						-Xloggc:logs/gc.log
						-XX:GCLogFileSize=10M
						-XX:NumberOfGCLogFiles=10
						-XX:+UseGCLogFileRotation
						-XX:+PrintGCDateStamps
						-XX:+PrintGCTimeStamps
						-XX:+PrintGCDetails
						-XX:+PrintHeapAtGC
						-XX:+PrintGCApplicationStoppedTime
						-XX:+DisableExplicitGC
						-verbose:gc
					
				
			
		
	
	
		
			generate-jsw-scripts
			package
			
				generate-daemons
			
		
	

5.6 maven-assembly-plugin

maven-assembly-plugin插件用于将上述appassembler-maven-plugin插件打包后的目录框架,再次打包为压缩包,便于在不同环境中进行拷贝操作。



	org.apache.maven.plugins
	maven-assembly-plugin
	3.1.0
	
		
			src/main/resources/tools/assembly.xml
		
	
	
		
			make-assembly
			package
			
				single
			
		
	

assembly.xml文件内容为:


    dist
    
        tar.gz
    
    true

    
        
            target/dist/jsw/app/bin
            bin
            
            0755
            0755
        
        
            target/dist/jsw/app/conf/tools
            bin
            0755
            0755
            
                **.sh
                **.bat
            
        
        
            target/dist/jsw/app/conf
            conf
            
            
                
                *.yml
                *.xml
                *.properties
                static/**
                
                *.conf
            
            
                
                tools/**
            
            0644
            0744
        
        
            target/dist/jsw/app/lib
            lib
            0644
            0744
        
        
            target/dist/jsw/app/logs
            logs
            0644
            0744
        
        
            target/dist/jsw/app/tmp
            tmp
            0644
            0744
        
    

5.7 打包命令

mvn clean package appassembler:generate-daemons -Dmaven.test.skip=true

打包之后的目录结构为:

微服务架构—JAVA打包黑科技_第4张图片微服务架构—JAVA打包黑科技_第5张图片微服务架构—JAVA打包黑科技_第6张图片

转载于:https://my.oschina.net/yu120/blog/1933594

你可能感兴趣的:(微服务架构—JAVA打包黑科技)