idea通过maven打包(三)

前言

现在开发的项目多为maven管理。熟悉idea通过maven打包流程及常见问题解决。

一、打包流程

1.1idea通过maven打包项目spring-boot项目

准备工作:
idea已经配置好maven
springboot项目
打包步骤:
1.在pom.xml文件的build标签下添加如下内容:

 

 

第一个plugin标签内容用于打包
第二个plugin标签内容用于指定源码jdk版本(maven的默认编译使用的jdk版本貌似很低)
2.点击idea右侧的Maven,打开Lifecycle目录,双击目录下的package或install即可将项目打包。
package和install的区别:
package 只是打包到target下, install是打包安装到本地maven仓库。

 

 3.运行jar包,即可验证是否成功。
注:
(1)若是部署项目,本地又无法连接到远程的数据库。打包项目时可能会报错。
此时只需要在pom文件中加入如下代码,即可跳过数据库连接检查,直接打包。

 

 (2)关于生成jar包的命名:
与name标签内内容+version标签内的内容一致。

 1.2生命周期的说法

maven将项目的生命周期大体分为9个,分别为:clean、validate、compile、test、package、verify、install、site、deploy 
我常常用的也就是clean、compile、package、install、deploy,并且deploy相对也较少,由于不多发布公共的项目供别人依赖使用,基本也就是项目打包为war时候会打包到私服,运维人员能够到私服上直接下载对应版本。 
其中clean即清除项目中编译文件和本地仓库中已打包的文件(即本地install的文件,install后面讲到) 
compile即编译项目中的java文件,并存放在项目的编译目录(根据不一样的配置,编译目录也不同) 
test 即运行项目中的测试用例文件,若是测试用例未经过,也会打包失败,另,这里的test过程能够在pom中经过配置跳过。(想一想也是,我项目都好了,其实不是非要跑测试用例的) 
package 即将本地编译好的文件打包为war 或者jar(这是最多见的两种,其余相关自行了解) 
verify 我不多用到,没怎么了解过 
install 将打包的代码存放到本地maven仓库,可供本地其它项目依赖使用 。
site生成项目报告,站点,发布站点,这个也不多用到,不是很清楚 。
deploy 将打包在本地仓库中的项目发布到服务器,供他人依赖使用 。
详细生命周期自行学习了解。

maven在执行一个生命周期的命令的时候将会执行以前的全部生命周期操做,好比执行mvn install,会执行前面一系列的动做包括 compile , package , test 等,具体请查看maven的官方文档。这个特性使maven的命令更加简洁易用。

参看链接:

一般常用的方式:https://blog.csdn.net/qq_39312230/article/details/103969312

通过配置的方式打war包部署到tomcat上:https://blog.csdn.net/liqi_q/article/details/80482602

1.2配置文件

pom.xml中需要添加如下配置:

 <!-- 插件配置 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <addMavenDescriptor>true</addMavenDescriptor>
                        <index>true</index>
                        <manifest>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>${skip_maven_test}</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>${skip_maven_deploy}</skip>
                </configuration>
            </plugin>
            <!-- install插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <configuration>
                    <skip>${skip_maven_install}</skip>
                </configuration>
            </plugin>
            <!-- git 流程管理 -->
            <plugin>
                <groupId>com.amashchenko.maven.plugin</groupId>
                <artifactId>gitflow-maven-plugin</artifactId>
                <version>${gitflow.version}</version>
                <configuration>
                    <installProject>false</installProject>
                    <skipTestProject>true</skipTestProject>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>**/*</include>
                        </includes>
                        <excludes>
                            <exclude>*.yml</exclude>
                            <exclude>logback-spring.xml</exclude>
                        </excludes>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>**/*</include>
                        </includes>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>

其中标黄的部分是打包相关的插件。profile标签用来说明是用的哪个yml配置文件。这么项目的打包插件用到了pdfc自己集成的一些东西。

1.3本地通过java -jar命令启动

1.3.1 java命令执行jar包带环境变量

java -jar reins-monitor-backend-1.0.jar --jasypt.encryptor.password=asd
或
java -Djasypt.encryptor.password=asd -jar reins-monitor-backend-1.0.jar 

参看链接:https://blog.csdn.net/weixin_39720807/article/details/114355543

1.3.2 spring-boot项目的jar文件读取配置文件的方式

会先读取项目根路径下的配置文件,找不到会读取jar包目录下的。

二、常见问题

2.1 报错一

SpringBoot启动时报org.springframework.context.annotation.ConflictingBeanDefinitionException

这是因为在应用中使用到了多个该类的对象,而该类的对象通过注解的方式注入到应用中,在注入的过程中因为对象的名称重复导致了该异常 
通过指定注入对象的名称可以解决这个问题。

参看链接:https://blog.csdn.net/u013360850/article/details/77622629/

我报这个错的原因是因为在打包前,没有编译,还是编译有误。。。导致生成一个重复的class文件,重新编译后,打包,问题解决。

com.picc.reins.hdrdomain.domain.zrj.facade.ZrjDao
com.picc.reins.hdrdomain.domain.zrj.repository.facade.ZrjDao

报错信息:

[2021-08-25 17:26:41.610] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [c.u.j.c.EnableEncryptablePropertiesConfiguration] [] [] [] [Bootstraping jasypt-string-boot auto configuration in context: application-1]
[2021-08-25 17:26:41.611] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [com.picc.reins.ReinsMonitorBackend] [] [] [] [No active profile set, falling back to default profiles: default]
[2021-08-25 17:26:41.627] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] [] [] [] [Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4e41089d: startup date [Wed Aug 25 17:26:41 CST 2021]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@649d209a]
[2021-08-25 17:26:42.710] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [o.s.d.r.config.RepositoryConfigurationDelegate] [] [] [] [Multiple Spring Data modules found, entering strict repository configuration mode!]
[2021-08-25 17:26:42.740] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [o.s.d.r.config.RepositoryConfigurationDelegate] [] [] [] [Multiple Spring Data modules found, entering strict repository configuration mode!]
[2021-08-25 17:26:43.088] [main] [ERROR] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [org.springframework.boot.SpringApplication] [] [] [] [Application run failed]
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'zrjDao' for bean class [com.picc.reins.hdrdomain.domain.zrj.facade.ZrjDao] conflicts with existing, non-compatible bean definition of same name and class [com.picc.reins.hdrdomain.domain.zrj.repository.facade.ZrjDao]
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:348)
        at org.mybatis.spring.mapper.ClassPathMapperScanner.checkCandidate(ClassPathMapperScanner.java:310)
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:286)
        at org.mybatis.spring.mapper.ClassPathMapperScanner.doScan(ClassPathMapperScanner.java:204)
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:253)
        at org.mybatis.spring.mapper.MapperScannerConfigurer.postProcessBeanDefinitionRegistry(MapperScannerConfigurer.java:375)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:121)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
        at com.picc.reins.ReinsMonitorBackend.main(ReinsMonitorBackend.java:22)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
[2021-08-25 17:26:43.089] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] 
[o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] [] [] [] [Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4e41089d: 
startup date [Wed Aug 25 17:26:41 CST 2021]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@649d209a]
View Code

2.2报错二

报错信息:

[2021-08-25 17:52:13.782] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [c.u.j.encryptor.DefaultLazyEncryptor] [] [] [] [Encryptor config not found for property jasypt.encryptor.saltGeneratorClassname, using default value: org.jasypt.salt.RandomSaltGenerator]
[2021-08-25 17:52:13.784] [main] [INFO ] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [c.u.j.encryptor.DefaultLazyEncryptor] [] [] [] [Encryptor config not found for property jasypt.encryptor.stringOutputType, using default value: base64]
[2021-08-25 17:52:14.233] [main] [ERROR] [TID: N/A] [LAPTOP-MHHBQEIO/11.204.117.183] [] [com.alibaba.druid.pool.DruidDataSource] [] [] [] [init datasource error, url: jdbc:informix-sqli://10.133.200.14:9032/newreinsdb:informixserver=newreins_hdr;NEWCODESET=gbk,8859-1,819]
java.sql.SQLException: com.informix.asf.IfxASFException: Code-set conversion function failed due to illegal sequence or invalid value.
        at com.informix.jdbc.IfxSqliConnect.<init>(IfxSqliConnect.java:1664)
        at com.informix.jdbc.IfxDriver.connect(IfxDriver.java:200)
        at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1643)
        at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1709)
        at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:932)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
View Code

原因:jar包存放目录含有中文。

三、延伸

3.1Idea工具将SpringBoot项目打成可执行jar包

参看链接:

https://blog.csdn.net/wsf408908184/article/details/81486363

https://blog.csdn.net/weixin_42074377/article/details/80884081

posted @ 2021-08-26 00:57  慎终若始  阅读(1855)  评论(0编辑  收藏  举报