Skip to content

Commit

Permalink
modify plugin export with classes as default (#819)
Browse files Browse the repository at this point in the history
* support delegate from classLoaderHook for getResourceAsStream

* Revert "support delegate from classLoaderHook for getResourceAsStream"

This reverts commit 5bd306b.

* fix typo

* plugin default export package without activator, modify to set

* fix format

* export class for plugin as default
  • Loading branch information
lvjing2 committed Jan 3, 2024
1 parent 9c5ce37 commit ef9584d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,6 @@ public static String getPackageName(String className) {
return Constants.DEFAULT_PACKAGE;
}

/**
* find common package among classes
* @param classNames class name list
* @return common package path
*/
public static Set<String> findCommonPackage(Set<String> classNames) {
Set<String> packages = new HashSet<>();

if (classNames == null || classNames.isEmpty()) {
return new HashSet<>(packages);
}

classNames.forEach(className -> packages.add(getPackageName(className)));
// delete default package
packages.remove(".");
return new HashSet<>(packages);
}

/**
* find all compiled classes in dir, ignore inner, anonymous and local classes
* @param dir directory that stores class files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ public void testGetPackageName() {
assertEquals(DEFAULT_PACKAGE, getPackageName("C"));
}

@Test
public void testFindCommonPackage() {
assertEquals(findCommonPackage(null).size(), 0);
Set<String> classNames = new HashSet<>();
classNames.add("com.example.project.subpackage1.classE");
classNames.add("com.example.project.classA");
classNames.add("com.example.project.classB");
classNames.add("com.example.project.subpackage.classC");
classNames.add("com.example.project.subpackage.classD");
assertEquals(findCommonPackage(classNames).size(), 3);
classNames.add("org.apache.util.ClassF");
assertEquals(findCommonPackage(classNames).size(), 4);
}

@Test
public void testCollectClasses() throws Exception {

Expand All @@ -65,6 +51,5 @@ public void testCollectClasses() throws Exception {

Set<String> classNames = new HashSet<>(collectClasses(dir2));
assertTrue(classNames.contains("com.alipay.sofa.ark.common.util.ClassUtils"));
assertTrue(findCommonPackage(classNames).contains("com.alipay.sofa.ark.common.util"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public abstract class AbstractPropertiesConfig {
*/
protected LinkedHashSet<String> resources;

public void addPackage(String pack) {
if (packages == null) {
packages = new LinkedHashSet<>();
public void addClass(String className) {
if (classes == null) {
classes = new LinkedHashSet<>();
}
packages.add(pack);
classes.add(className);
}

public String getMode() {
Expand Down Expand Up @@ -125,4 +125,4 @@ public static String join(Iterator iterator, String separator) {
*/
public abstract void store(Properties props);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public class ArkPluginMojo extends AbstractMojo {
* Export plugin project classes by default
*/
@Parameter(defaultValue = "true")
protected Boolean exportPackage;
protected Boolean exportPluginClass;

private static final String ARCHIVE_MODE = "zip";
private static final String PLUGIN_SUFFIX = ".ark.plugin";
Expand Down Expand Up @@ -413,8 +413,8 @@ protected boolean isAttach() {
*
* @return whether to export plugin project
*/
protected boolean isExportPackage() {
return exportPackage;
protected boolean getExportPluginClass() {
return exportPluginClass;
}

/**
Expand Down Expand Up @@ -449,19 +449,19 @@ private Properties collectArkPluginExport() throws MojoExecutionException {
if (exported == null) {
exported = new ExportConfig();
}
if (exportPackage) {
Set<String> projectPackages = findProjectPackages();
for (String projectPackage : projectPackages) {
if (!StringUtils.isEmpty(projectPackage)) {
exported.addPackage(projectPackage + ".*");
if (exportPluginClass) {
Set<String> projectClasses = findProjectClasses();
for (String projectClass : projectClasses) {
if (!StringUtils.isEmpty(projectClass)) {
exported.addClass(projectClass);
}
}
}
exported.store(properties);
return properties;
}

private Set<String> findProjectPackages() throws MojoExecutionException {
private Set<String> findProjectClasses() throws MojoExecutionException {
try {
// Accessing the target/classes directory where compiled classes are located
File outputDirectory = new File(project.getBuild().getOutputDirectory());
Expand All @@ -470,7 +470,7 @@ private Set<String> findProjectPackages() throws MojoExecutionException {
Set<String> classes = new HashSet<>(ClassUtils.collectClasses(outputDirectory));
classes = classes.stream().filter(className -> !className.equals(this.activator)).collect(
Collectors.toSet());
return ClassUtils.findCommonPackage(classes);
return classes;
} else {
getLog().warn("Output directory does not exist!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<description>default ark plugin artifact classifier: empty</description>
</parameter>
<parameter>
<name>exportPackage</name>
<name>exportClass</name>
<type>java.lang.Boolean</type>
<required>false</required>
<editable>true</editable>
Expand Down Expand Up @@ -630,4 +630,4 @@
<version>3.3.1</version>
</dependency>
</dependencies>
</plugin>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void cleanUp() throws IOException {
arkPluginMojo.pluginName = "xxx";
arkPluginMojo.description = "yyy";
arkPluginMojo.workDirectory = new File("./");
arkPluginMojo.exportPackage = true;
arkPluginMojo.exportPluginClass = true;
arkPluginMojo.execute();
assertEquals(6, finalResourcesCountInJar.get());
}
Expand Down
1 change: 0 additions & 1 deletion sofa-ark-plugin/config-ark-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@

<configuration>
<activator>com.alipay.sofa.ark.config.ConfigBaseActivator</activator>
<exportPackage>false</exportPackage>
</configuration>
</execution>
</executions>
Expand Down
1 change: 0 additions & 1 deletion sofa-ark-plugin/netty-ark-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<package>org.apache.*</package>
</packages>
</exported>
<exportPackage>false</exportPackage>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit ef9584d

Please sign in to comment.