Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack files from jar if jar is used, copy then extracted files to a directory

I have a large webapp which uses many Maven dependencies. They are included as JAR files, but I want to have a chance to use some of them as an opened project directly in Eclipse. Then dependent projects are linked with m2e.

From some of that JARs/projects, resources need to be extracted.

How can I do that with Maven-dependency-plugin? If artifact is included as JAR, unpack it, and then copy files to required directory. If artifact is included as project, it exists on a harddrive and files can be directly accessed and copied, without unpack.

like image 577
Xdg Avatar asked Sep 13 '13 13:09

Xdg


People also ask

How do I unpack a JAR file?

Select “File,” then click “Open Archive” to open your JAR file, or click the “Open” icon. Browse to find the file's location, select it, and then click “Open.”

Can we extract files from jar?

The jar-file argument is the filename (or path and filename) of the JAR file from which to extract files. archived-file(s) is an optional argument consisting of a space-separated list of the files to be extracted from the archive. If this argument is not present, the Jar tool will extract all the files in the archive.

Do jar files need to be extracted?

The JVM is capable of loading classes or files from a jar file without extracting the jar to temp files. This functionality is also available to you in the standard library, see the JarFile for more information. So no, the JVM does not extract a jar to temp files, classes (and resources) are simply loaded on demand.

How many files can be extracted from a JAR file?

As many files as desired can be extracted from the JAR file in the same way. When the command doesn't specify which files to extract, the Jar tool extracts all files in the archive. For example, you can extract all the files in the TicTacToe archive by using this command:

How to extract a JAR file in Linux command line?

If you only want to extract a specific file from the “.jar” archive, you can use the “jar xvf” command followed by the name of the file you want to extract. That’s all there is to extracting “.jar” files using the Linux command line. JAR files can be dragged and dropped from the main window of JD-GUI or directly into the file’s location.

How do I copy a JAR file to another jar file?

Copy the JAR file's path. Press ⌘ Command +⌥ Option + C to do so. This will copy the file path to the JAR file along with the JAR file's name and extension.

What is the difference between JAR-file and archived-file (s)?

The jar-fileargument is the filename (or path and filename) of the JAR file from which to extract files. archived-file(s)is an optional argument consisting of a space-separated list of the files to be extracted from the archive.


1 Answers

The m2e-plugin can neither execute the maven dependency plugin nor copy sources on its own.

You can use the maven dependency-plugin to extract files from jar:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>yourSourceGroup</groupId>
                                <artifactId>yourSourceArtifact</artifactId>
                                <version>1.0.0</version>
                                <type>jar</type>
                                <includes>path/to/Files.whatsoever</includes>
                                <outputDirectory>${project.build.directory}/your/target/folder</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

(You may have to change phase or goal for your needs.)

You can run "rightclick->Run as-> Maven install" on your project from inside eclipse to copy the files to the place you need them.

As an alternative for all this you can use a resource filter in your webapp to get resources directly from jars at runtime, e. g. from spring framework.

like image 106
Volker Seibt Avatar answered Sep 29 '22 05:09

Volker Seibt