-
Notifications
You must be signed in to change notification settings - Fork 49
Add Archiver that creates modular JAR files using the JDK jar tool #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
plamentotev
wants to merge
5
commits into
codehaus-plexus:master
from
plamentotev:modular-jars-jar-tool
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
42892e8
Bump version to 3.6 for the upcoming changes
plamentotev 24444fe
Add new method (postCreateArchive) to AbstractArchiver
plamentotev 38e2880
Add base test class for JAR archivers
plamentotev 54fe62b
Add base class for creating modular JAR files.
plamentotev 3c2dc6a
Add Archiver that creates modular JAR files using the JDK jar tool.
plamentotev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 256 additions & 0 deletions
256
src/main/java/org/codehaus/plexus/archiver/jar/JarToolModularJarArchiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
/** | ||
* | ||
* Copyright 2018 The Apache Software Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.codehaus.plexus.archiver.jar; | ||
|
||
import org.apache.commons.compress.parallel.InputStreamSupplier; | ||
import org.codehaus.plexus.archiver.ArchiverException; | ||
import org.codehaus.plexus.archiver.util.ArchiveEntryUtils; | ||
import org.codehaus.plexus.archiver.util.ResourceUtils; | ||
import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator; | ||
import org.codehaus.plexus.components.io.resources.PlexusIoResource; | ||
import org.codehaus.plexus.util.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* A {@link ModularJarArchiver} implementation that uses | ||
* the {@code jar} tool provided by | ||
* {@code java.util.spi.ToolProvider} to create | ||
* modular JAR files. | ||
* | ||
* <p> | ||
* The basic JAR archive is created by {@link JarArchiver} | ||
* and the {@code jar} tool is used to upgrade it to modular JAR. | ||
* | ||
* <p> | ||
* If the JAR file does not contain module descriptor | ||
* or the JDK does not provide the {@code jar} tool | ||
* (for example JDK prior to Java 9), then the | ||
* archive created by {@link JarArchiver} | ||
* is left unchanged. | ||
*/ | ||
public class JarToolModularJarArchiver | ||
extends ModularJarArchiver | ||
{ | ||
private static final String MODULE_DESCRIPTOR_FILE_NAME | ||
= "module-info.class"; | ||
|
||
private static final Pattern MRJAR_VERSION_AREA | ||
= Pattern.compile( "META-INF/versions/\\d+/" ); | ||
|
||
private Object jarTool; | ||
|
||
private boolean moduleDescriptorFound; | ||
|
||
private Path tempDir; | ||
|
||
public JarToolModularJarArchiver() | ||
{ | ||
try | ||
{ | ||
Class<?> toolProviderClass = | ||
Class.forName( "java.util.spi.ToolProvider" ); | ||
Object jarToolOptional = toolProviderClass | ||
.getMethod( "findFirst", String.class ) | ||
.invoke( null, "jar" ); | ||
|
||
jarTool = jarToolOptional.getClass().getMethod( "get" ) | ||
.invoke( jarToolOptional ); | ||
} | ||
catch ( ReflectiveOperationException | SecurityException e ) | ||
{ | ||
// Ignore. It is expected that the jar tool | ||
// may not be available. | ||
} | ||
} | ||
|
||
@Override | ||
protected void zipFile( InputStreamSupplier is, ConcurrentJarCreator zOut, | ||
String vPath, long lastModified, File fromArchive, | ||
int mode, String symlinkDestination, | ||
boolean addInParallel ) | ||
throws IOException, ArchiverException | ||
{ | ||
// We store the module descriptors in temporary location | ||
// and then add it to the JAR file using the JDK jar tool. | ||
// It may look strange at first, but to update a JAR file | ||
// you need to add new files[1] and the only files | ||
// we're sure that exists in modular JAR file | ||
// are the module descriptors. | ||
// | ||
// [1] There are some exceptions but we need at least one file to | ||
// ensure it will work in all cases. | ||
if ( jarTool != null && isModuleDescriptor( vPath ) ) | ||
{ | ||
getLogger().debug( "Module descriptor found: " + vPath ); | ||
|
||
moduleDescriptorFound = true; | ||
|
||
// Copy the module descriptor to temporary directory | ||
// so later then can be added to the JAR archive | ||
// by the jar tool. | ||
|
||
if ( tempDir == null ) | ||
{ | ||
tempDir = Files | ||
.createTempDirectory( "plexus-archiver-modular_jar-" ); | ||
tempDir.toFile().deleteOnExit(); | ||
} | ||
|
||
File destFile = tempDir.resolve( vPath ).toFile(); | ||
destFile.getParentFile().mkdirs(); | ||
destFile.deleteOnExit(); | ||
|
||
ResourceUtils.copyFile( is.get(), destFile ); | ||
ArchiveEntryUtils.chmod( destFile, mode ); | ||
destFile.setLastModified( lastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE | ||
? System.currentTimeMillis() | ||
: lastModified ); | ||
} | ||
else | ||
{ | ||
super.zipFile( is, zOut, vPath, lastModified, | ||
fromArchive, mode, symlinkDestination, addInParallel ); | ||
} | ||
} | ||
|
||
@Override | ||
protected void postCreateArchive() | ||
throws ArchiverException | ||
{ | ||
if ( !moduleDescriptorFound ) | ||
{ | ||
// no need to update the JAR archive | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
getLogger().debug( "Using the jar tool to " + | ||
"update the archive to modular JAR." ); | ||
|
||
Integer result = (Integer) jarTool.getClass() | ||
.getMethod( "run", | ||
PrintStream.class, PrintStream.class, String[].class ) | ||
.invoke( jarTool, | ||
System.out, System.err, | ||
getJarToolArguments() ); | ||
|
||
if ( result != null && result != 0 ) | ||
{ | ||
throw new ArchiverException( "Could not create modular JAR file. " + | ||
"The JDK jar tool exited with " + result ); | ||
} | ||
} | ||
catch ( ReflectiveOperationException | SecurityException e ) | ||
{ | ||
throw new ArchiverException( "Exception occurred " + | ||
"while creating modular JAR file", e ); | ||
} | ||
finally | ||
{ | ||
clearTempDirectory(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns {@code true} if {@code path} | ||
* is a module descriptor. | ||
*/ | ||
private boolean isModuleDescriptor( String path ) | ||
{ | ||
if ( path.endsWith( MODULE_DESCRIPTOR_FILE_NAME ) ) | ||
{ | ||
String prefix = path.substring( 0, | ||
path.lastIndexOf( MODULE_DESCRIPTOR_FILE_NAME ) ); | ||
|
||
// the path is a module descriptor if it located | ||
// into the root of the archive or into the | ||
// version are of a multi-release JAR file | ||
return prefix.isEmpty() || | ||
MRJAR_VERSION_AREA.matcher( prefix ).matches(); | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Prepares the arguments for the jar tool. | ||
* It takes into account the module version, | ||
* main class, etc. | ||
*/ | ||
private String[] getJarToolArguments() | ||
{ | ||
List<String> args = new ArrayList<>(); | ||
|
||
args.add( "--update" ); | ||
args.add( "--file" ); | ||
args.add( getDestFile().getAbsolutePath() ); | ||
|
||
if ( getModuleMainClass() != null ) | ||
{ | ||
args.add( "--main-class" ); | ||
args.add( getModuleMainClass() ); | ||
} | ||
|
||
if ( getModuleVersion() != null ) | ||
{ | ||
args.add( "--module-version" ); | ||
args.add( getModuleVersion() ); | ||
} | ||
|
||
if ( !isCompress() ) | ||
{ | ||
args.add( "--no-compress" ); | ||
} | ||
|
||
args.add( "-C" ); | ||
args.add( tempDir.toFile().getAbsolutePath() ); | ||
args.add( "." ); | ||
|
||
return args.toArray( new String[]{} ); | ||
} | ||
|
||
/** | ||
* Makes best effort the clean up | ||
* the temporary directory used. | ||
*/ | ||
private void clearTempDirectory() | ||
{ | ||
try | ||
{ | ||
if ( tempDir != null ) | ||
{ | ||
FileUtils.deleteDirectory( tempDir.toFile() ); | ||
} | ||
} | ||
catch ( IOException e ) | ||
{ | ||
// Ignore. It is just best effort. | ||
} | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/org/codehaus/plexus/archiver/jar/ModularJarArchiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* | ||
* Copyright 2018 The Apache Software Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.codehaus.plexus.archiver.jar; | ||
|
||
/** | ||
* Base class for creating modular JAR archives. | ||
* | ||
* Subclasses are required to be able to handle both | ||
* JAR archives with module descriptor (modular JAR) | ||
* and without ("regular" JAR). | ||
* That would allow clients of this class to use | ||
* it without prior knowledge if the classes | ||
* they are going to add are part of module | ||
* (contain module descriptor class) or not. | ||
* | ||
* @since 3.6 | ||
*/ | ||
public abstract class ModularJarArchiver | ||
extends JarArchiver | ||
{ | ||
private String moduleMainClass; | ||
|
||
private String moduleVersion; | ||
|
||
public String getModuleMainClass() | ||
{ | ||
return moduleMainClass; | ||
} | ||
|
||
/** | ||
* Sets the module main class. | ||
* Ignored if the JAR file does not contain | ||
* module descriptor. | ||
* | ||
* <p>Note that implementations may choose | ||
* to replace the value set in the manifest as well. | ||
* | ||
* @param moduleMainClass the module main class. | ||
*/ | ||
public void setModuleMainClass( String moduleMainClass ) | ||
{ | ||
this.moduleMainClass = moduleMainClass; | ||
} | ||
|
||
public String getModuleVersion() | ||
{ | ||
return moduleVersion; | ||
} | ||
|
||
/** | ||
* Sets the module version. | ||
* Ignored if the JAR file does not contain | ||
* module descriptor. | ||
* | ||
* @param moduleVersion the module version. | ||
*/ | ||
public void setModuleVersion( String moduleVersion ) | ||
{ | ||
this.moduleVersion = moduleVersion; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do 3.6.0-SNAPSHOT instead ? I would suggest to have three digits.. ? WDYT ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last couple of releases are two digits. I don't know if there is any particular reason - I think @michael-o did the last release. Maybe he knows. Other than that I agree that with three digits is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have agreed at some point to use semver with three digits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, then I'll change it to 3.6.0