Skip to content

Commit

Permalink
gradle precompile plugin supports pattern match feature for package
Browse files Browse the repository at this point in the history
filters
  • Loading branch information
xiemalin committed Nov 17, 2020
1 parent ed10550 commit 1d794d6
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 8 deletions.
9 changes: 6 additions & 3 deletions Document.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Maven插件支持预编译功能配置,使用该功能后,所有的Jprotobuf
<plugin>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf-precompile-plugin</artifactId>
<version>1.2.8</version>
<version>1.3.4</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
<filterClassPackage>com.baidu</filterClassPackage>
Expand All @@ -82,6 +82,7 @@ Maven插件支持预编译功能配置,使用该功能后,所有的Jprotobuf
</plugin>
```
filterClassPackage 用来指定进行预编译时需要扫描的package,目前只支持配置多个package名称,使用";"分隔<br>
1.3.4与2.2.4版本以后,支持通配符方式,如包名定义 com.baidu.student.pk1*
generateProtoFile 设置是否开启proto文件生成,默认为false,不生成
compileDependencies 开启依赖的class编译,默认为true, 开启
maven执行命令如下:<br>
Expand All @@ -97,7 +98,7 @@ mvn package
```property
plugins {
id 'com.baidu.jprotobuf' version '1.0.8'
id 'com.baidu.jprotobuf' version '1.0.9'
}
```
或者使用以下方式
Expand All @@ -110,7 +111,7 @@ buildscript {
}
}
dependencies {
classpath "gradle.plugin.com.baidu.jprotobuf:jprotobuf-precompile-plugin-gradle:1.0.8"
classpath "gradle.plugin.com.baidu.jprotobuf:jprotobuf-precompile-plugin-gradle:1.0.9"
}
}
Expand All @@ -126,6 +127,8 @@ jprotobuf_precompile {
generateProtoFile="false" // 设置是否怎么生成proto描述文件, 默认是false, 不生成
}
```
####备注 1.0.9版本之后,gradle插件, filterClassPackage支持通配符匹配规则,例如 filterClassPackage="com.mytest.pkg*"


gradle 执行命令如下:<br>
```property
Expand Down
2 changes: 1 addition & 1 deletion jprotobuf-precompile-plugin-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ javadoc {
}
}

version = "1.0.8" //配置插件的版本号
version = "1.0.9" //配置插件的版本号
group = "com.baidu.jprotobuf" //插件的群组,插件的classpath会用到

pluginBundle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ private static String printUsage() {
}

/**
* Checks if is start with.
* Checks if is start with or pattern match with.
*
* @param testString the test string
* @param targetStrings the target strings
* @return true, if is start with
*/
private static boolean isStartWith(String testString, String[] targetStrings) {
for (String s : targetStrings) {
if (testString.startsWith(s)) {
if (testString.startsWith(s) || PatternMatchUtils.simpleMatch(s, testString)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.baidu.jprotobuf.plugin;

/**
* Utility methods for simple pattern matching, in particular for Spring's typical "xxx*", "*xxx" and "*xxx*" pattern
* styles.
*
* @author Juergen Hoeller
* @since 2.0
*/
public abstract class PatternMatchUtils {

/**
* Match a String against the given pattern, supporting the following simple pattern styles: "xxx*", "*xxx", "*xxx*"
* and "xxx*yyy" matches (with an arbitrary number of pattern parts), as well as direct equality.
*
* @param pattern the pattern to match against
* @param str the String to match
* @return whether the String matches the given pattern
*/
public static boolean simpleMatch(String pattern, String str) {
if (pattern == null || str == null) {
return false;
}
int firstIndex = pattern.indexOf('*');
if (firstIndex == -1) {
return pattern.equals(str);
}
if (firstIndex == 0) {
if (pattern.length() == 1) {
return true;
}
int nextIndex = pattern.indexOf('*', firstIndex + 1);
if (nextIndex == -1) {
return str.endsWith(pattern.substring(1));
}
String part = pattern.substring(1, nextIndex);
if ("".equals(part)) {
return simpleMatch(pattern.substring(nextIndex), str);
}
int partIndex = str.indexOf(part);
while (partIndex != -1) {
if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) {
return true;
}
partIndex = str.indexOf(part, partIndex + 1);
}
return false;
}
return (str.length() >= firstIndex && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex))
&& simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));
}

/**
* Match a String against the given patterns, supporting the following simple pattern styles: "xxx*", "*xxx",
* "*xxx*" and "xxx*yyy" matches (with an arbitrary number of pattern parts), as well as direct equality.
*
* @param patterns the patterns to match against
* @param str the String to match
* @return whether the String matches any of the given patterns
*/
public static boolean simpleMatch(String[] patterns, String str) {
if (patterns != null) {
for (String pattern : patterns) {
if (simpleMatch(pattern, str)) {
return true;
}
}
}
return false;
}

}
2 changes: 1 addition & 1 deletion jprotobuf-precompile-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf-precompile-plugin</artifactId>
<version>1.4.2</version>
<version>1.4.3</version>
<packaging>maven-plugin</packaging>
<name>jprotobuf-precompile-plugin</name>
<description>This plugin is to do compile jprotobuf pojo class after compile execution</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private static String printUsage() {
*/
private static boolean isStartWith(String testString, String[] targetStrings) {
for (String s : targetStrings) {
if (testString.startsWith(s)) {
if (testString.startsWith(s) || PatternMatchUtils.simpleMatch(s, testString)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.baidu.jprotobuf.mojo;

/**
* Utility methods for simple pattern matching, in particular for Spring's typical "xxx*", "*xxx" and "*xxx*" pattern
* styles.
*
* @author Juergen Hoeller
* @since 2.0
*/
public abstract class PatternMatchUtils {

/**
* Match a String against the given pattern, supporting the following simple pattern styles: "xxx*", "*xxx", "*xxx*"
* and "xxx*yyy" matches (with an arbitrary number of pattern parts), as well as direct equality.
*
* @param pattern the pattern to match against
* @param str the String to match
* @return whether the String matches the given pattern
*/
public static boolean simpleMatch(String pattern, String str) {
if (pattern == null || str == null) {
return false;
}
int firstIndex = pattern.indexOf('*');
if (firstIndex == -1) {
return pattern.equals(str);
}
if (firstIndex == 0) {
if (pattern.length() == 1) {
return true;
}
int nextIndex = pattern.indexOf('*', firstIndex + 1);
if (nextIndex == -1) {
return str.endsWith(pattern.substring(1));
}
String part = pattern.substring(1, nextIndex);
if ("".equals(part)) {
return simpleMatch(pattern.substring(nextIndex), str);
}
int partIndex = str.indexOf(part);
while (partIndex != -1) {
if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) {
return true;
}
partIndex = str.indexOf(part, partIndex + 1);
}
return false;
}
return (str.length() >= firstIndex && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex))
&& simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));
}

/**
* Match a String against the given patterns, supporting the following simple pattern styles: "xxx*", "*xxx",
* "*xxx*" and "xxx*yyy" matches (with an arbitrary number of pattern parts), as well as direct equality.
*
* @param patterns the patterns to match against
* @param str the String to match
* @return whether the String matches any of the given patterns
*/
public static boolean simpleMatch(String[] patterns, String str) {
if (patterns != null) {
for (String pattern : patterns) {
if (simpleMatch(pattern, str)) {
return true;
}
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.baidu.bjf.remoting.protobuf.v3.any;

0 comments on commit 1d794d6

Please sign in to comment.