18
18
19
19
import org .apache .commons .compress .parallel .InputStreamSupplier ;
20
20
import org .codehaus .plexus .archiver .ArchiverException ;
21
- import org .codehaus .plexus .archiver .util .ArchiveEntryUtils ;
22
- import org .codehaus .plexus .archiver .util .ResourceUtils ;
23
21
import org .codehaus .plexus .archiver .zip .ConcurrentJarCreator ;
24
- import org .codehaus .plexus .components .io .resources .PlexusIoResource ;
25
- import org .codehaus .plexus .util .FileUtils ;
26
22
27
23
import java .io .File ;
28
24
import java .io .IOException ;
29
25
import java .io .PrintStream ;
30
26
import java .nio .file .Files ;
31
- import java .nio .file .Path ;
32
27
import java .util .ArrayList ;
33
28
import java .util .List ;
34
29
import java .util .regex .Pattern ;
@@ -63,8 +58,6 @@ public class JarToolModularJarArchiver
63
58
64
59
private boolean moduleDescriptorFound ;
65
60
66
- private Path tempDir ;
67
-
68
61
public JarToolModularJarArchiver ()
69
62
{
70
63
try
@@ -92,47 +85,15 @@ protected void zipFile( InputStreamSupplier is, ConcurrentJarCreator zOut,
92
85
boolean addInParallel )
93
86
throws IOException , ArchiverException
94
87
{
95
- // We store the module descriptors in temporary location
96
- // and then add it to the JAR file using the JDK jar tool.
97
- // It may look strange at first, but to update a JAR file
98
- // you need to add new files[1] and the only files
99
- // we're sure that exists in modular JAR file
100
- // are the module descriptors.
101
- //
102
- // [1] There are some exceptions but we need at least one file to
103
- // ensure it will work in all cases.
104
88
if ( jarTool != null && isModuleDescriptor ( vPath ) )
105
89
{
106
90
getLogger ().debug ( "Module descriptor found: " + vPath );
107
91
108
92
moduleDescriptorFound = true ;
109
-
110
- // Copy the module descriptor to temporary directory
111
- // so later then can be added to the JAR archive
112
- // by the jar tool.
113
-
114
- if ( tempDir == null )
115
- {
116
- tempDir = Files
117
- .createTempDirectory ( "plexus-archiver-modular_jar-" );
118
- tempDir .toFile ().deleteOnExit ();
119
- }
120
-
121
- File destFile = tempDir .resolve ( vPath ).toFile ();
122
- destFile .getParentFile ().mkdirs ();
123
- destFile .deleteOnExit ();
124
-
125
- ResourceUtils .copyFile ( is .get (), destFile );
126
- ArchiveEntryUtils .chmod ( destFile , mode );
127
- destFile .setLastModified ( lastModified == PlexusIoResource .UNKNOWN_MODIFICATION_DATE
128
- ? System .currentTimeMillis ()
129
- : lastModified );
130
- }
131
- else
132
- {
133
- super .zipFile ( is , zOut , vPath , lastModified ,
134
- fromArchive , mode , symlinkDestination , addInParallel );
135
93
}
94
+
95
+ super .zipFile ( is , zOut , vPath , lastModified ,
96
+ fromArchive , mode , symlinkDestination , addInParallel );
136
97
}
137
98
138
99
@ Override
@@ -163,15 +124,11 @@ protected void postCreateArchive()
163
124
"The JDK jar tool exited with " + result );
164
125
}
165
126
}
166
- catch ( ReflectiveOperationException | SecurityException e )
127
+ catch ( IOException | ReflectiveOperationException | SecurityException e )
167
128
{
168
129
throw new ArchiverException ( "Exception occurred " +
169
130
"while creating modular JAR file" , e );
170
131
}
171
- finally
172
- {
173
- clearTempDirectory ();
174
- }
175
132
}
176
133
177
134
/**
@@ -203,7 +160,20 @@ private boolean isModuleDescriptor( String path )
203
160
* main class, etc.
204
161
*/
205
162
private String [] getJarToolArguments ()
163
+ throws IOException
206
164
{
165
+ // We add empty temporary directory to the JAR file.
166
+ // It may look strange at first, but to update a JAR file
167
+ // you need to add new files[1]. If we add empty directory
168
+ // it will be ignored (not added to the archive), but
169
+ // the module descriptor will be updated and validated.
170
+ //
171
+ // [1] There are some exceptions (such as when the main class
172
+ // is updated) but we need at least empty directory
173
+ // to ensure it will work in all cases.
174
+ File tempEmptyDir = Files .createTempDirectory ( null ).toFile ();
175
+ tempEmptyDir .deleteOnExit ();
176
+
207
177
List <String > args = new ArrayList <>();
208
178
209
179
args .add ( "--update" );
@@ -228,29 +198,10 @@ private String[] getJarToolArguments()
228
198
}
229
199
230
200
args .add ( "-C" );
231
- args .add ( tempDir . toFile () .getAbsolutePath () );
201
+ args .add ( tempEmptyDir .getAbsolutePath () );
232
202
args .add ( "." );
233
203
234
204
return args .toArray ( new String []{} );
235
205
}
236
206
237
- /**
238
- * Makes best effort the clean up
239
- * the temporary directory used.
240
- */
241
- private void clearTempDirectory ()
242
- {
243
- try
244
- {
245
- if ( tempDir != null )
246
- {
247
- FileUtils .deleteDirectory ( tempDir .toFile () );
248
- }
249
- }
250
- catch ( IOException e )
251
- {
252
- // Ignore. It is just best effort.
253
- }
254
- }
255
-
256
207
}
0 commit comments