Java 如果使用 jar,则从 jar 中解压文件,然后将提取的文件复制到目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18788030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unpack files from jar if jar is used, copy then extracted files to a directory
提问by Xdg
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.
我有一个大型 web 应用程序,它使用了许多 Maven 依赖项。它们作为 JAR 文件包含在内,但我希望有机会直接在 Eclipse 中将其中一些用作打开的项目。然后依赖项目与 m2e 链接。
From some of that JARs/projects, resources need to be extracted.
从其中一些 JAR/项目中,需要提取资源。
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.
我如何使用 Maven-dependency-plugin 做到这一点?如果工件以 JAR 形式包含,则解压它,然后将文件复制到所需目录。如果artifact作为项目包含在硬盘上,可以直接访问和复制文件,无需解包。
采纳答案by Volker Seibt
The m2e-plugin can neither execute the maven dependency plugin nor copy sources on its own.
m2e-plugin 既不能执行 maven 依赖插件,也不能自己复制源。
You can use the maven dependency-plugin to extract files from jar:
您可以使用 maven 依赖插件从 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.
您可以从 eclipse 内部对您的项目运行“右键单击-> 运行方式-> Maven 安装”以将文件复制到您需要它们的位置。
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.
作为所有这些的替代方案,您可以在 web 应用程序中使用资源过滤器在运行时直接从 jar 获取资源,例如从 spring 框架。
回答by AfroSuperman
Download the JD-GUI executable, run it and it should unpack all your jar files regardless of how they were build. Download link: http://www.softpedia.com/get/Programming/Debuggers-Decompilers-Dissasemblers/?utm_source=spd&utm_campaign=postdl_redir
下载 JD-GUI 可执行文件,运行它,它应该会解压所有的 jar 文件,无论它们是如何构建的。下载链接:http: //www.softpedia.com/get/Programming/Debuggers-Decompilers-Dissasemblers/?utm_source=spd&utm_campaign= postdl_redir