First, the application scenarios
We generally used for maven springboot when springboot Application Packaging maven plug springboot-maven-plugin package, to give a complete package advent of the FatJar, the advent of the FatJar advantage can be run directly, the disadvantage is too bulky, not conducive to the transmission, springboot application to break out of fatjar volume ranging from dozens of M, as many as hundreds of M, is then deployed to the server when the transmission may only change a certain class files will need to retransmit the entire fatjar once, especially to go public transport when possible upload speed of only a few hundred or even tens of KB, and the entire fatjar really our code files or projects may also hundreds of megabytes in size KB, it is necessary to rely fatjar in our library class project were separated package, so each class replacement project a lot easier, and the reason will be separated from the profile that we may often need to change the contents of the configuration file, if placed in fatjar this amendment is very inconvenient, so it needs to be separated from the configuration file.
All dependent libraries and configuration files scored a jar or war> fatjar the upcoming needs of the project, the file can be run directly
Second, the configuration
2.1 POM configuration
Hereinafter pom.xml be configured to achieve separation package, configured as follows
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>chenyb</groupId> <artifactId>demo</artifactId> <version>v1.2-release</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- springboot 打包插件 --> <!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.xx.xx</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> --> <!-- maven 打包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> < The artifactId > maven-jar-plugin</ the artifactId > < Configuration > < Archive > < the manifest > < addClassPath > to true </ addClassPath > <-! The MANIFEST.MF prefix added in the Class-Path -> < classpathPrefix > lib / </ classpathPrefix > < ! - JAR package does not include a version identifier unique -> < useUniqueVersions > to false </ useUniqueVersions > <-! specify the entry class -> <mainClass >cn.test.DemoApplication</mainClass> </manifest> </archive> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </plugin> <!-- 拷贝依赖 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>true</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
The key configuration instructions:
(1) remove the spring-boot-maven-plugin package widget
(2) adding maven-jar-plugin (maven standard widget package)
(3) maven-dependency-plugin (copy dependent plug and is used to copy out maven dependencies)
Plug-specific configuration, pom.xml has been added Remarks
2.2 Packaging
Execution command maven package to package, following results were obtained
The lib directory and project jar copy the file to the same directory, in order to facilitate testing me, first of all copied to the desktop (when placed on the server also need to ensure that in the same directory)
Open demo-v1.2-release can be seen, and do not depend jar comes in, the size of less than 4KB
2.3 config directory creation
Above also need to finish out the project to copy configuration files, and establishing jar package same level directory config directory , the project application.properties yaml or copy files come in
Config file under
After the above steps, the entire configuration is complete, the following conduct some simple tests
Third, the test
In order to ensure that the configuration file is loaded external config directory, application-test.yaml I will server.port changed in 8085, open a command line input
C:\Users\Administrator\Desktop>java -jar -Dspring.profiles.active=dev -Dspring.location.config=config/ C:\Users\Administrator\Desktop\demo-v1.2-release.jar
Enter run, normal startup instructions come in the normal external dependencies can be loaded
We can see the complete start tomcat listening port to 8085 indicate that external configuration is loaded successfully.
PS: If the external configuration file fails to load, the project will use the jar in the configuration file, as shown below, that is, the start will be 8080 port
Port configured application-dev.yaml 8080
The application-dev.yaml under I have an external port config directory made changes, the use of an external configuration file to start would be 8085 port
Fourth, pits
By default, the command-line window is opened, the user is in the current directory, like this
And my config, lib, project jar copy on the desktop, the actual path is
I started in C: execution under the direct command \ Users \ Administrator>, has been less than load profile
java -jar -Dspring.profiles.active=dev -Dspring.location.config=config/ C:\Users\Administrator\Desktop\demo-v1.2-release.jar
The reason is that under the program and configuration files are not in the same directory, I'm in C: \ Users \ Administrator> Run the start command, and the program the actual directory in C: \ Users \ Administrator \ Desktop> next, because the program uses the absolute path can be found files, so the actual program is run path C: \ Users \ Administrator \ Desktop, but I used to configure spring.location.config = config / ,, using a relative path relative path is relative to C: \ Users \ Administrator > directory, so you can not find the configuration file will appear.
Solution one:
Command-line switch to the C: \ Users \ Administrator \ Desktop directory, directory project jar, run java -jar command
Solution two:
The config copied to the C: / Users / Administrator, to ensure the C: config directories and configuration files exist / Users / Administrator relative to the lower path (the method solves the problem, but not recommended)
Solution three:
spring.location.config = config / the absolute path, i.e. C: / Users / Administrator / Desktop / config /
So a very important point, we must ensure the implementation of the project directory with the jar command, lib, config in the same directory.
Fifth, the full demo address
https://github.com/yuboon/java-examples/tree/master/springboot-package-segment