Skip to content

Commit

Permalink
Remove all jars from ant class path except of core
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Dec 9, 2024
1 parent 6a3ec61 commit 87d6b34
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ not generate anything.

Each Zserio extension should be packed in a single jar file.

All Zserio extensions which are available on the Java classpath are automatically loaded during Zserio compiler
All Zserio extensions which are available on the Java classpath or which are located in the directory
from which the Zserio jar executable has been run, are automatically loaded during Zserio compiler
startup.

More information how to implement a new Zserio extension can be found in the
Expand Down
20 changes: 6 additions & 14 deletions ant_task/src/zserio/ant/ToolWrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zserio.ant;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -40,25 +41,16 @@ public ToolWrapper(String className, Iterable<Path> classPath, boolean ignoreErr
String [] files = p.list();
for (String f : files)
{
String u = null;
try
{
if (f.endsWith(".jar"))
{
u = "jar:file:"+f+"!/";
}
else
{
u = "file:"+f+"/";
}

System.out.println("Adding " + u + " to classpath");
urls.add(new URL(u));
final URL pathUrl = new File(f).toURI().toURL();
System.out.println("Adding " + pathUrl + " to classpath");
urls.add(pathUrl);

}
catch (MalformedURLException e)
catch (MalformedURLException | RuntimeException e)
{
throw new BuildException("Malformed URL: " + u);
throw new BuildException("Malformed URL from file: " + f + " (" + e + ")");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/core/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spotbugs is
<attribute name="package" default="zserio.antlr"/>
<sequential>
<echo message="Generating ANTLR 4 grammar: @{target}"/>
<java classname="org.antlr.v4.Tool" failonerror="true">
<java classname="org.antlr.v4.Tool" failonerror="true" fork="true">
<classpath>
<pathelement location="${3rdparty.jar_dir}/${3rdparty.antlr4.jar_file_name}"/>
</classpath>
Expand Down
24 changes: 13 additions & 11 deletions compiler/core/src/zserio/tools/ExtensionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -105,11 +107,11 @@ public void callExtensions(Root rootNode, ExtensionParameters parameters) throws
private ClassLoader getClassLoader()
{
final ClassLoader currentClassLoader = getClass().getClassLoader();
final String workingDirectory = getWorkingDirectory();
final File workingDirectory = getWorkingDirectory();
if (workingDirectory == null)
return currentClassLoader;

final File[] fileList = new File(workingDirectory).listFiles();
final File[] fileList = workingDirectory.listFiles();
if (fileList == null)
return currentClassLoader;

Expand All @@ -120,7 +122,7 @@ private ClassLoader getClassLoader()
{
if (isFileZserioExtension(file))
{
urlArray.add(new URL("file:" + file.getPath()));
urlArray.add(file.toURI().toURL());
urlArray.addAll(getDependentJarsFromManifest(file));
}
}
Expand All @@ -136,17 +138,17 @@ private ClassLoader getClassLoader()
return urlClassLoader;
}

private String getWorkingDirectory()
private File getWorkingDirectory()
{
try
{
final String execPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
final String decodedExecPath = URLDecoder.decode(execPath, "UTF-8");
final String workingDirectory = new File(decodedExecPath).getParent();
final URI execUri = getClass().getProtectionDomain().getCodeSource().getLocation().toURI();
final Path execPath = Paths.get(execUri);
final Path execParentPath = execPath.getParent();

return workingDirectory;
return (execParentPath == null) ? null : execParentPath.toFile();
}
catch (SecurityException | UnsupportedEncodingException excpt)
catch (SecurityException | URISyntaxException excpt)
{
return null;
}
Expand Down
7 changes: 4 additions & 3 deletions test/core/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spo
<zserio srcPath="${@{testName}.zs.in_dir}" srcFile="@{zsFile}" ignoreError="@{ignoreErrors}"
extraArgs="${zserio.extra_args}">
<classpath>
<fileset dir="${zserio.jar_dir}">
<include name="*.jar"/>
</fileset>
<file name="${zserio.jar_dir}/zserio_core.jar"/>
<file name="${zserio.jar_dir}/antlr-4.7.2-complete.jar"/>
<file name="${zserio.jar_dir}/commons-cli-1.4.jar"/>
<file name="${zserio.jar_dir}/freemarker-2.3.28.jar"/>
</classpath>
<args/>
</zserio>
Expand Down
7 changes: 4 additions & 3 deletions test/extensions/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spo
<zserio srcPath="${@{testName}.zs.in_dir}" srcFile="@{zsFile}" ignoreError="@{ignoreErrors}"
extraArgs="${zserio.extra_args}">
<classpath>
<fileset dir="${zserio.jar_dir}">
<include name="*.jar"/>
</fileset>
<file name="${zserio.jar_dir}/zserio_core.jar"/>
<file name="${zserio.jar_dir}/antlr-4.7.2-complete.jar"/>
<file name="${zserio.jar_dir}/commons-cli-1.4.jar"/>
<file name="${zserio.jar_dir}/freemarker-2.3.28.jar"/>
</classpath>
<arg name="java" value="${@{testName}.zs.out_dir}"/>
<args/>
Expand Down

0 comments on commit 87d6b34

Please sign in to comment.