美文网首页
springboot启动分包

springboot启动分包

作者: go_2021 | 来源:发表于2022-01-07 22:40 被阅读0次

耐心是一切聪明才智的基础。

先试试一个单纯的maven项目,不加springbootnew出来一个maven项目,在pom.xml里引入hutool包,项目main方法里调用hutool包内的方法。直接maven打包为jar,运行:java -jar xx.jar报错:

java -jar /Users/yawei/IdeaProjects/testJar/target/testJar-1.0-SNAPSHOT.jar
/Users/yawei/IdeaProjects/testJar/target/testJar-1.0-SNAPSHOT.jar中没有主清单属性

看一下jar包里的manifest.mf文件:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: yawei
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_181

没有指定Main-Class所以会报错,pom中加入对应配置:

    <build>
        <finalName>aaaaaa</finalName><!--修改编译出来的jar包名,仅为{artifactId}.jar-->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.AA</mainClass> <!-- 此处为主入口-->
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

这时manifest.mf文件中加入了:Main-Class: com.AA,但是还是报错:

Exception in thread "main" java.lang.NoClassDefFoundError: cn/hutool/captcha/CaptchaUtil

看来还得把依赖的jar加到运行jar包中,再加个多jar合一的插件,运行成功:

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- put your configurations here -->
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

下面来试试springboot的项目,这次分开jar运行:

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.example.demo.DemoApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

还有一个非常重要的一步,就是添加依赖:

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
        </dependency>
image.png

maven install之后会在lib生成需要的依赖包,并生成项目jar包。此时运行命令成功运行:

java -jar /Users/yawei/Downloads/demo2/target/demo-0.0.1-SNAPSHOT.jar

看一下生成的业务jar包里的manifest.mf内容:

Manifest-Version: 1.0
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Built-By: yawei
Implementation-Vendor-Id: com.example
Class-Path: lib/spring-boot-starter-web-2.0.3.RELEASE.jar lib/spring-b
 oot-starter-2.0.3.RELEASE.jar 。。。
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_181
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
 ot-starter-parent/demo
Main-Class: com.example.demo.DemoApplication

在Class-Path:中指定了各个jar包的位置。在实际工作当中在测试服项目jar包所在目录创建lib文件夹把这些jar放进去,每次只改业务代码的话,就install,把项目jar上传到测试就可以了。
在实际项目当中,遇到了一个问题:Failed to load class [javax.servlet.Filter]
参考这个链接改好了把provided改为compilehttps://blog.csdn.net/weixin_43960336/article/details/109076134

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

之前每次打包jar有个100M左右,现在只有100kb,秒构建,秒部署,美滋滋。

后续(一共3个项目其中一个报错了。。):
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cpsController': Unsatisfied dependency expressed through field 'openService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'open' defined in URL [jar:file:/Users/yawei/IdeaProjects/ae/ae-plat/target/ae-plat-0.0.1-SNAPSHOT.jar!/com/bz365/plat/service/cps/impl/OpenServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class com.bz365.plat.service.cps.impl.OpenServiceImpl: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null

代码是没有改的,用一个大jar包的方式启动肯定是没问题的。为啥分包就报错了呢?从提示来看是生成cpsController的时候报错了,重试了bean字段注入改为构造方法注入还是报错,在启动类加入注解:@EnableAspectJAutoProxy依然报错,感觉陷入了一个巨坑,看不到一点光明。。
经过我不懈的努力,不断重试,终于让项目跑起来了。😀😁。。
解决方法是吧,springboot的devtools这狗依赖删除就可以了:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.0.3.RELEASE</version>
            <optional>true</optional>
        </dependency>

相关文章

网友评论

      本文标题:springboot启动分包

      本文链接:https://www.haomeiwen.com/subject/wayjcrtx.html