解决screw Maven插件方式运行时,在编译打包时跳过执行的问题
问题描述
在引入screw 进行数据库文档生成,并且使用Maven插件方式运行时,在执行项目打包时,会自动运行该插件。导致在不需要重新生成数据库文档时,也会去生成数据库文档。并且通常数据库文档生成是基于开发环境的数据库的,对于非开发环境的自动打包处理环境,往往是无法访问开发环境数据库,这也会导致项目的打包出错的问题。出错信息如下:
不带-X
参数的出错信息:
[INFO] --- screw-maven-plugin:1.0.5:run (default) @ java-abc-cids-interfaces ---
[INFO] Database design document generation begins 🚀
[INFO] screw - Starting...
Build step 'Invoke top-level Maven targets' marked build as failure
带-X
参数的出错信息:
[INFO] Database design document generation begins 🚀
[DEBUG] Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader ClassRealm[plugin>cn.smallbun.screw:screw-maven-plugin:1.0.5, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@ba8a1dc]
[DEBUG] screw - configuration:
[DEBUG] allowPoolSuspension.............false
[DEBUG] autoCommit......................true
[DEBUG] catalog.........................none
[DEBUG] connectionInitSql...............none
[DEBUG] connectionTestQuery.............none
[DEBUG] connectionTimeout...............30000
[DEBUG] dataSource......................none
[DEBUG] dataSourceClassName.............none
[DEBUG] dataSourceJNDI..................none
[DEBUG] dataSourceProperties............{useInformationSchema=true, password=<masked>}
[DEBUG] driverClassName................."com.mysql.cj.jdbc.Driver"
[DEBUG] exceptionOverrideClassName......none
[DEBUG] healthCheckProperties...........{}
[DEBUG] healthCheckRegistry.............none
[DEBUG] idleTimeout.....................600000
[DEBUG] initializationFailTimeout.......1
[DEBUG] isolateInternalQueries..........false
[DEBUG] jdbcUrl.........................jdbc:mysql://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[DEBUG] leakDetectionThreshold..........0
[DEBUG] maxLifetime.....................1800000
[DEBUG] maximumPoolSize.................10
[DEBUG] metricRegistry..................none
[DEBUG] metricsTrackerFactory...........none
[DEBUG] minimumIdle.....................10
[DEBUG] password........................<masked>
[DEBUG] poolName........................"screw"
[DEBUG] readOnly........................false
[DEBUG] registerMbeans..................false
[DEBUG] scheduledExecutor...............none
[DEBUG] schema..........................none
[DEBUG] threadFactory...................internal
[DEBUG] transactionIsolation............default
[DEBUG] username........................"xxxxx"
[DEBUG] validationTimeout...............5000
[INFO] screw - Starting...
[DEBUG] screw - Failed to create/setup connection: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
[DEBUG] screw - Cannot acquire connection from data source
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException (SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException (SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init> (ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance (ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:197)
at com.zaxxer.hikari.util.DriverDataSource.getConnection (DriverDataSource.java:138)
at com.zaxxer.hikari.pool.PoolBase.newConnection (PoolBase.java:358)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry (PoolBase.java:206)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry (HikariPool.java:477)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast (HikariPool.java:560)
at com.zaxxer.hikari.pool.HikariPool.<init> (HikariPool.java:115)
at com.zaxxer.hikari.HikariDataSource.<init> (HikariDataSource.java:81)
at cn.smallbun.screw.maven.plugin.mojo.RunDocMojo.getDataSource (RunDocMojo.java:274)
at cn.smallbun.screw.maven.plugin.mojo.RunDocMojo.execute (RunDocMojo.java:197)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:10 min
[INFO] Finished at: 2023-03-20T13:23:57+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal cn.smallbun.screw:screw-maven-plugin:1.0.5:run (default) on project java-abc-cids-interfaces: Execution default of goal cn.smallbun.screw:screw-maven-plugin:1.0.5:run failed: Failed to initialize pool: Communications link failure
[ERROR]
[ERROR] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.: connect timed out
[ERROR] -> [Help 1]
调研
通常的对于Maven插件跳过使用,我们只需要找插件的对应的skip
参数,然后在打包时通过-D
参数来修改这个参数即可,但通过阅读这个插件的源码,并且开启Maven的-X
参数执行,都没有在这个插件中找到skip
参数。
通过查看代码得知,插件的默认触发执行的Phase
是COMPILE
,因此解决的思路就有了。
解决方案
方案1
注释掉插件引入代码,在开发环境需要手动执行的时候,再放开注释。
方案2
不再写死配置的Phase ,而是指定一个变量,由运行Maven命令时,通过参数-D
传入正确的Phase
,而不需要执行的时候,传入一个不存在的Phase
或者干脆不传入Phase
即可。
修改前:
<plugin>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-maven-plugin</artifactId>
<version>1.0.5</version>
<dependencies>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<configuration>
... 省略
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
修改后:
...
<plugin>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-maven-plugin</artifactId>
<version>1.0.5</version>
<dependencies>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<configuration>
... 省略
</configuration>
<executions>
<execution>
<phase>${mvn.screw.skip}</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Maven 运行的方法:
- 不需要运行
clean package -Dmvn.screw.skip=none -f pom.xml
或者 clean package -f pom.xml
- 需要运行
clean package -Dmvn.screw.skip=compile -f pom.xml
- 单独运行:
screw:run -f pom.xml