目录
- 0、前言
- 1、enforcer 是什么
- 2、能干什么
- 3、怎么用
- 4、规则
- 5、扩展规则
- 6、使用
- 7、banDuplicateClasses
- 8、banDuplicatePomDependencyVersions
0、前言
maven 项目种经常出现 jar 包冲突、重复依赖、无效引用怎么办,maven-enforcer-plugin 了解一下
1、enforcer 是什么
Enforcer is a build rule execution framework
提供了管控一系列环境约束的目标,包括但不限于Maven版本、JDK版本和操作系统家族等多个内置规则以及用户自定义规则。
https://maven.apache.org/enforcer/maven-enforcer-plugin/index.html
2、能干什么
在项目构建时,通过规则来约束项目配置,如jdk 、maven 版本,是否有重复的类,文件编码,文件大小等等,且规则是可以自己扩展的
3、怎么用
https://maven.apache.org/enforcer/maven-enforcer-plugin/usage.html
4、规则
https://maven.apache.org/enforcer/enforcer-rules/index.html
5、扩展规则
https://www.mojohaus.org/extra-enforcer-rules/index.html#
6、使用
pom 中配置如下插件
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>3.0.0-M3</version><executions><execution><id>enforce-versions</id><goals><goal>enforce</goal></goals><configuration><rules><bannedPlugins><!-- will only display a warning but does not fail the build. --><level>WARN</level><excludes><exclude>org.apache.maven.plugins:maven-verifier-plugin</exclude></excludes><message>Please consider using the maven-invoker-plugin (http://maven.apache.org/plugins/maven-invoker-plugin/)!</message></bannedPlugins><banDuplicateClasses><ignoreClasses><!-- example of ignoring one specific class --><ignoreClass>com.xyz.i18n.Messages</ignoreClass><!-- example of ignoring with wildcards --><ignoreClass>org.apache.commons.logging.*</ignoreClass></ignoreClasses><scopes><scope>compile</scope><scope>provided</scope></scopes><findAllDuplicates>true</findAllDuplicates><ignoreWhenIdentical>true</ignoreWhenIdentical></banDuplicateClasses><requireMavenVersion><version>3.2.0</version></requireMavenVersion><requireJavaVersion><version>1.8</version></requireJavaVersion></rules><fail>false</fail></configuration></execution></executions><dependencies><dependency><groupId>org.codehaus.mojo</groupId><artifactId>extra-enforcer-rules</artifactId><version>1.2</version></dependency></dependencies>
</plugin>
7、banDuplicateClasses
重复类检测用的rule
<banDuplicateClasses><ignoreClasses><!-- example of ignoring one specific class --><ignoreClass>com.xyz.i18n.Messages</ignoreClass><!-- example of ignoring with wildcards --><ignoreClass>org.apache.commons.logging.*</ignoreClass></ignoreClasses><scopes><scope>compile</scope><scope>provided</scope></scopes><findAllDuplicates>true</findAllDuplicates><ignoreWhenIdentical>true</ignoreWhenIdentical>
</banDuplicateClasses>
执行 使用 mvn clean compile
即可
结果输出,可重点关注 Duplicat classes 部分
[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (enforce-versions) @ tet-app ---
[INFO] artifact com.test.maven.test1:com.test.mavne.test1.contract: checking for updates from nexus-public-snapshots
[INFO] artifact com.test.maven.test1:com.test.mavne.test1.contract: checking for updates from nexus
[INFO] Adding ignore: module-info
[INFO] Adding ignore: META-INF/versions/*/module-info
[INFO] Adding ignore: com.xyz.i18n.Messages
[INFO] Adding ignore: org.apache.commons.logging.*
[WARNING] Rule 1: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message:
Duplicate classes found:Found in:org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.34:providedorg.glassfish:javax.el:jar:3.0.0:compileDuplicate classes:javax/el/LambdaExpression.classjavax/el/ValueExpression.classjavax/el/ExpressionFactory.classjavax/el/MapELResolver.classjavax/el/ELClass.classjavax/el/PropertyNotFoundException.classjavax/el/FunctionMapper.classjavax/el/ELResolver.classjavax/el/BeanNameResolver.classjavax/el/BeanNameELResolver.classjavax/el/CompositeELResolver.classjavax/el/ELManager.classjavax/el/EvaluationListener.classjavax/el/ResourceBundleELResolver.classjavax/el/ListELResolver.classjavax/el/BeanELResolver$BeanProperty.classjavax/el/ArrayELResolver.classjavax/el/MethodInfo.classjavax/el/StandardELContext.classjavax/el/VariableMapper.classjavax/el/ValueReference.classjavax/el/MethodExpression.classjavax/el/ELProcessor.classjavax/el/TypeConverter.classjavax/el/ELContextEvent.classjavax/el/Expression.classjavax/el/BeanELResolver.classjavax/el/BeanELResolver$1.classjavax/el/ELException.classjavax/el/StaticFieldELResolver.classjavax/el/ELContext.classjavax/el/ImportHandler.classjavax/el/PropertyNotWritableException.classjavax/el/MethodNotFoundException.classjavax/el/BeanELResolver$BeanProperties.classFound in:org.apache.xbean:xbean-reflect:jar:3.7:compileorg.unidal.framework:foundation-service:jar:2.5.0:compileDuplicate classes:org/apache/xbean/recipe/ObjectRecipe$Property.classorg/apache/xbean/recipe/ObjectRecipe$Member.classorg/apache/xbean/recipe/ObjectRecipe.classorg/apache/xbean/recipe/ObjectRecipe$MethodMember.classorg/apache/xbean/recipe/ObjectRecipe$CompoundProperty.classorg/apache/xbean/recipe/ObjectRecipe$AutoMatchProperty.classorg/apache/xbean/recipe/ObjectRecipe$SetterProperty.classorg/apache/xbean/recipe/ObjectRecipe$FieldProperty.classorg/apache/xbean/recipe/ObjectRecipe$FieldMember.classFound in:org.codehaus.plexus:plexus-container-default:jar:1.6:compileorg.unidal.framework:foundation-service:jar:2.5.0:compileDuplicate classes:org/codehaus/plexus/component/builder/XBeanComponentBuilder$RequirementRecipe.classorg/codehaus/plexus/component/builder/XBeanComponentBuilder.classorg/codehaus/plexus/component/builder/XBeanComponentBuilder$1.classorg/codehaus/plexus/component/builder/XBeanComponentBuilder$PlexusConfigurationRecipe.classFound in:javax.servlet:servlet-api:jar:2.5:compileorg.apache.tomcat.embed:tomcat-embed-core:jar:8.5.34:providedDuplicate classes:javax/servlet/http/Cookie.classjavax/servlet/http/HttpSessionContext.classjavax/servlet/http/HttpSessionListener.classjavax/servlet/http/HttpSessionAttributeListener.classjavax/servlet/ServletRequestAttributeListener.classjavax/servlet/ServletRequest.classjavax/servlet/http/HttpUtils.classjavax/servlet/FilterConfig.classjavax/servlet/ServletResponseWrapper.classjavax/servlet/ServletResponse.classjavax/servlet/SingleThreadModel.classjavax/servlet/ServletConfig.classjavax/servlet/http/HttpSessionBindingEvent.classjavax/servlet/ServletOutputStream.classjavax/servlet/ServletContextListener.classjavax/servlet/Filter.classjavax/servlet/Servlet.classjavax/servlet/ServletContextEvent.classjavax/servlet/http/NoBodyOutputStream.classjavax/servlet/UnavailableException.classjavax/servlet/ServletRequestListener.classjavax/servlet/http/HttpServletRequest.classjavax/servlet/ServletContext.classjavax/servlet/FilterChain.classjavax/servlet/http/HttpServletRequestWrapper.classjavax/servlet/http/HttpSessionBindingListener.classjavax/servlet/ServletRequestAttributeEvent.classjavax/servlet/ServletRequestWrapper.classjavax/servlet/ServletRequestEvent.classjavax/servlet/http/HttpServlet.classjavax/servlet/http/HttpServletResponseWrapper.classjavax/servlet/http/HttpSessionActivationListener.classjavax/servlet/http/NoBodyResponse.classjavax/servlet/GenericServlet.classjavax/servlet/ServletException.classjavax/servlet/ServletContextAttributeEvent.classjavax/servlet/http/HttpServletResponse.classjavax/servlet/ServletContextAttributeListener.classjavax/servlet/http/HttpSession.classjavax/servlet/RequestDispatcher.classjavax/servlet/http/HttpSessionEvent.classjavax/servlet/ServletInputStream.classFound in:com.sun.mail:javax.mail:jar:1.4.4:compilejavax.mail:mail:jar:1.4.5:compileDuplicate classes:com/sun/mail/iap/Atom.classcom/sun/mail/util/logging/MailHandler$TailNameFormatter.classcom/sun/mail/pop3/WritableSharedFile.classjavax/mail/Session.classcom/sun/mail/imap/IMAPFolder.classjavax/mail/Multipart.classcom/sun/mail/imap/IMAPFolder$FetchProfileItem.classcom/sun/mail/pop3/POP3Folder.classcom/sun/mail/iap/Response.classcom/sun/mail/imap/IMAPFolder$17.classcom/sun/mail/util/logging/LogManagerProperties$1.classcom/sun/mail/imap/IMAPFolder$8.classjavax/mail/internet/MimeBodyPart.classcom/sun/mail/handlers/text_plain.classcom/sun/mail/handlers/text_xml.classcom/sun/mail/smtp/SMTPTransport$DigestMD5Authenticator.classcom/sun/mail/smtp/DigestMD5.classcom/sun/mail/util/logging/MailHandler.classcom/sun/mail/smtp/SMTPTransport$Authenticator.classcom/sun/mail/imap/protocol/FetchResponse.classcom/sun/mail/smtp/SMTPTransport$NtlmAuthenticator.classjavax/mail/internet/MimePartDataSource.classcom/sun/mail/imap/protocol/Namespaces$Namespace.classcom/sun/mail/auth/Ntlm.classcom/sun/mail/iap/Argument.classcom/sun/mail/pop3/POP3Message.classcom/sun/mail/imap/IMAPFolder$5.classcom/sun/mail/imap/IMAPFolder$14.classjavax/mail/util/ByteArrayDataSource.classcom/sun/mail/imap/MessageLiteral.classjavax/mail/internet/MimeBodyPart$MimePartDataHandler.classcom/sun/mail/pop3/TempFile.classjavax/mail/util/SharedFileInputStream$SharedFile.classcom/sun/mail/imap/IMAPStore.classcom/sun/mail/imap/protocol/RFC822DATA.classcom/sun/mail/imap/IMAPStore$1.classcom/sun/mail/util/MailSSLSocketFactory.classcom/sun/mail/smtp/SMTPTransport.classcom/sun/mail/imap/IMAPFolder$15.classcom/sun/mail/smtp/SMTPTransport$PlainAuthenticator.classcom/sun/mail/util/MimeUtil$1.classjavax/mail/internet/InternetAddress.classcom/sun/mail/imap/IMAPFolder$6.classcom/sun/mail/imap/IMAPBodyPart.classjavax/mail/Transport.classcom/sun/mail/imap/IMAPFolder$7.classcom/sun/mail/imap/protocol/BODYSTRUCTURE.classcom/sun/mail/imap/IMAPFolder$16.classcom/sun/mail/imap/IMAPMessage.classcom/sun/mail/imap/protocol/IMAPProtocol.classcom/sun/mail/util/SocketFetcher$1.classcom/sun/mail/iap/Protocol.classcom/sun/mail/util/logging/LogManagerProperties.classcom/sun/mail/imap/IMAPFolder$1.classcom/sun/mail/imap/IMAPFolder$19.classjavax/mail/util/SharedFileInputStream.classjavax/mail/internet/MimeUtility.classcom/sun/mail/pop3/AppendStream.classcom/sun/mail/imap/IMAPFolder$4.classcom/sun/mail/util/UUEncoderStream.classcom/sun/mail/imap/protocol/IMAPSaslAuthenticator.classjavax/mail/internet/MimeUtility$1NullInputStream.classcom/sun/mail/imap/IMAPFolder$10.classcom/sun/mail/imap/IMAPFolder$13.classcom/sun/mail/imap/IMAPFolder$18.classcom/sun/mail/pop3/POP3Store.classcom/sun/mail/smtp/SMTPTransport$LoginAuthenticator.classcom/sun/mail/imap/protocol/Namespaces.classcom/sun/mail/pop3/Protocol.classcom/sun/mail/imap/IMAPFolder$11.classcom/sun/mail/util/MimeUtil.classcom/sun/mail/imap/IMAPFolder$2.classcom/sun/mail/imap/LengthCounter.classcom/sun/mail/imap/IMAPFolder$12.classcom/sun/mail/imap/IMAPMessage$1FetchProfileCondition.classcom/sun/mail/util/MailSSLSocketFactory$MailTrustManager.classjavax/mail/Folder.classcom/sun/mail/imap/protocol/RFC822SIZE.classcom/sun/mail/imap/IMAPFolder$3.classcom/sun/mail/imap/IMAPFolder$9.classcom/sun/mail/iap/AString.classjavax/mail/internet/MimeMessage.classcom/sun/mail/util/BASE64EncoderStream.classcom/sun/mail/imap/MessageCache.classcom/sun/mail/imap/IMAPStore$ConnectionPool.classjavax/mail/Version.classcom/sun/mail/util/SocketFetcher.classjavax/mail/internet/AsciiOutputStream.classjavax/mail/PasswordAuthentication.class[INFO]
[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (default-prepare-agent) @ test-app ---
[INFO] argLine set to -javaagent:/Users/test/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/Users/test/Develop/IdeaProjects/test-app/target/jacoco.exec -Xms512m -Xmx1024m
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ test-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 6 resources
[INFO] Copying 154 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1432 source files to /Users/test/Develop/IdeaProjects/test-app/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:28 min
[INFO] Finished at: 2024-02-26T23:44:18+08:00
[INFO] Final Memory: 68M/664M
[INFO] ------------------------------------------------------------------------
8、banDuplicatePomDependencyVersions
重复的pom 依赖配置 输出结果
[WARNING] Rule 3: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:
Found 2 duplicate dependency declaration in this project:- dependencies.dependency[com.daojia.mobile:com.daojia.mobile.signalbomb.contract:jar] ( 2 times )- dependencyManagement.dependencies.dependency[com.daojia.mobile:com.daojia.mobile.signalbomb.contract:jar] ( 2 times )
更多使用欢迎大家一起来发掘探讨。