Skip to content

Commit

Permalink
Export plugin project classes by default (#799)
Browse files Browse the repository at this point in the history
* feat: export plugin classes by default.

* feat: export plugin classes by default.

* feat: export plugin classes by default.

* feat: export plugin package by default.

* feat: export plugin package by default.

* default add all packages for plugin

* format

---------

Co-authored-by: yanhuai.yh <yanhuai.yh@antgroup.com>
Co-authored-by: leojames <leojames.googol@gmail.com>
  • Loading branch information
3 people committed Dec 5, 2023
1 parent 7de1384 commit 4ade973
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
package com.alipay.sofa.ark.common.util;

import com.alipay.sofa.ark.spi.constant.Constants;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author qilong.zql
Expand All @@ -43,6 +51,50 @@ 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 List<String> findCommonPackage(List<String> classNames) {
Set<String> packages = new HashSet<>();

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

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

/**
* find all compiled classes in dir, ignore inner, anonymous and local classes
* @param dir directory that stores class files
* @return compiled class names
*/
public static List<String> collectClasses(File dir) throws IOException {
List<String> classNames = new ArrayList<>();
Collection<File> classFiles = FileUtils.listFiles(dir, new String[] { "class" }, true);
String basePath = dir.getCanonicalPath();

for (File classFile : classFiles) {
// Get the relative file path starting after the classes directory
String relativePath = classFile.getCanonicalPath().substring(basePath.length() + 1);

// Convert file path to class name (replace file separators with dots and remove .class extension)
String className = relativePath.replace(File.separatorChar, '.').replaceAll(
"\\.class$", "");

// skip inner, anonymous and local classes
if (!className.contains("$")) {
classNames.add(className);
}
}
return classNames;
}

public static String getCodeBase(Class<?> cls) {

if (cls == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* @author qilong.zql
* @since 0.3.0
Expand All @@ -28,8 +32,36 @@ public class ClassUtilsTest {

@Test
public void testGetPackageName() {
Assert.assertTrue(ClassUtils.getPackageName("a.b.C").equals("a.b"));
Assert.assertTrue(ClassUtils.getPackageName("C").equals(Constants.DEFAULT_PACKAGE));
Assert.assertEquals("a.b", ClassUtils.getPackageName("a.b.C"));
Assert.assertEquals(Constants.DEFAULT_PACKAGE, ClassUtils.getPackageName("C"));
}

@Test
public void testFindCommonPackage() {
Assert.assertEquals(ClassUtils.findCommonPackage(null).size(), 0);
List<String> classNames = new ArrayList<>();
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");
Assert.assertEquals(ClassUtils.findCommonPackage(classNames).size(), 3);
classNames.add("org.apache.util.ClassF");
Assert.assertEquals(ClassUtils.findCommonPackage(classNames).size(), 4);
}

@Test
public void testCollectClasses() throws Exception {
File dir = new File("target/classes");
// fix mvn test fail issues
File dir2 = new File(dir.getAbsolutePath());
if (!dir2.exists()) {
return;
}

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

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

public String getMode() {
return mode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import com.alipay.sofa.ark.common.util.ClassUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.spi.constant.Constants;
import com.alipay.sofa.ark.tools.ArtifactItem;
Expand Down Expand Up @@ -143,6 +145,12 @@ public class ArkPluginMojo extends AbstractMojo {
@Parameter(defaultValue = "")
private String classifier;

/**
* Export plugin project classes by default
*/
@Parameter(defaultValue = "true")
protected Boolean exportPackage;

private static final String ARCHIVE_MODE = "zip";
private static final String PLUGIN_SUFFIX = ".ark.plugin";
private static final String TEMP_PLUGIN_SUFFIX = ".ark.plugin.bak";
Expand Down Expand Up @@ -398,6 +406,16 @@ protected boolean isAttach() {
return attach;
}

/**
* check whether to export plugin project
* default true.
*
* @return whether to export plugin project
*/
protected boolean isExportPackage() {
return exportPackage;
}

/**
* generate ark.plugin configuration file
* archive
Expand Down Expand Up @@ -425,15 +443,39 @@ private void addManifest(Archiver archiver) throws MojoExecutionException {
addArkPluginConfig(archiver, "META-INF/MANIFEST.MF", properties);
}

private Properties collectArkPluginExport() {
private Properties collectArkPluginExport() throws MojoExecutionException {
Properties properties = new LinkedProperties();
if (exported == null) {
exported = new ExportConfig();
}
if (exportPackage) {
List<String> projectPackages = findProjectPackages();
for (String projectPackage : projectPackages) {
if (!StringUtils.isEmpty(projectPackage)) {
exported.addPackage(projectPackage + ".*");
}
}
}
exported.store(properties);
return properties;
}

private List<String> findProjectPackages() throws MojoExecutionException {
try {
// Accessing the target/classes directory where compiled classes are located
File outputDirectory = new File(project.getBuild().getOutputDirectory());
// Ensure the directory exists
if (outputDirectory.exists()) {
return ClassUtils.findCommonPackage(ClassUtils.collectClasses(outputDirectory));
} else {
getLog().warn("Output directory does not exist!");
}
return new ArrayList<>();
} catch (IOException e) {
throw new MojoExecutionException("Error finding compiled classes", e);
}
}

private Properties collectArkPluginImport() {
Properties properties = new LinkedProperties();
if (imported == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
<editable>true</editable>
<description>default ark plugin artifact classifier: empty</description>
</parameter>
<parameter>
<name>exportPackage</name>
<type>java.lang.Boolean</type>
<required>false</required>
<editable>true</editable>
<description>Export plugin project package by default</description>
</parameter>
<parameter>
<name>description</name>
<type>java.lang.String</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.model.Build;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.AbstractArchiver;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
Expand Down Expand Up @@ -107,9 +108,12 @@ public void cleanUp() throws IOException {
defaultArtifact.setFile(new File("src/test/resources/test-demo.jar"));
artifacts.add(defaultArtifact);

Build build = new Build();
build.setOutputDirectory("./notexist");
MavenProject mavenProject = mock(MavenProject.class);
when(mavenProject.getArtifacts()).thenReturn(artifacts);
when(mavenProject.getArtifact()).thenReturn(defaultArtifact);
when(mavenProject.getBuild()).thenReturn(build);
arkPluginMojo.setProject(mavenProject);
arkPluginMojo.setShades(new LinkedHashSet<>(Collections
.singleton("com.alipay.sofa:test-demo:1.0.0")));
Expand All @@ -125,6 +129,7 @@ public void cleanUp() throws IOException {
arkPluginMojo.pluginName = "xxx";
arkPluginMojo.description = "yyy";
arkPluginMojo.workDirectory = new File("./");
arkPluginMojo.exportPackage = true;
arkPluginMojo.execute();
assertEquals(6, finalResourcesCountInJar.get());
}
Expand Down
1 change: 1 addition & 0 deletions sofa-ark-plugin/config-ark-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@

<configuration>
<activator>com.alipay.sofa.ark.config.ConfigBaseActivator</activator>
<exportPackage>false</exportPackage>
</configuration>
</execution>
</executions>
Expand Down
1 change: 1 addition & 0 deletions sofa-ark-plugin/netty-ark-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<package>org.apache.*</package>
</packages>
</exported>
<exportPackage>false</exportPackage>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 1 addition & 1 deletion sofa-ark-plugin/web-ark-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<package>javax.*</package>
<package>org.apache.*</package>
<!-- fix https://github.com/sofastack/sofa-ark/issues/737 -->
<package>com.alipay.sofa.ark.web.embed.*</package>
<!-- <package>com.alipay.sofa.ark.web.embed.*</package>-->
</packages>
</exported>
</configuration>
Expand Down

0 comments on commit 4ade973

Please sign in to comment.