执行jar命令排除文件

发布于 2024-10-10 09:12:13 字数 383 浏览 0 评论 0原文

我遵循了Java Archive Tool,但不能'找不到如何排除文件夹。

例如,我的工作副本目录下有文件:

workingcopy
--src
  --.svn
  -- com
--.svn
--WebContent

我只想从 src 下的类编译和创建 jar,不包括以 .svn 目录开头的所有文件夹。我想从 cmd 运行它,例如 jar -cf test.jar 等。

我该怎么做?

I followed The Java Archive Tool but couldn't find how to exclude folders.

For example, I have the files under working copy directory:

workingcopy
--src
  --.svn
  -- com
--.svn
--WebContent

I would like to compile and create jar only from the classes under src excluding all folders starting with .svn directory. I want to run it from cmd like jar -cf test.jar etc..

How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

花辞树 2024-10-17 09:12:13

这可以通过命令来完成:

jar cf test.jar `find . -not -path "*/.svn/*" -not -type d`

jar 的问题是,如果传递目录,它将递归地添加所有内容。因此,我们的目标是仅传递文件,并且仅传递路径中没有“.svn”子字符串的文件。为此,find 命令与 2 个条件一起使用:

  1. -not -path "*/.svn*" 过滤掉所有 svn 文件
  2. -not -type d< /code> 过滤掉所有目录

这将从 jar 中排除空目录。

This can be done with a command:

jar cf test.jar `find . -not -path "*/.svn/*" -not -type d`

The problem with jar is that if directory is passed it will be added recursively with all content. So our goal is to pass only files and only those of them which doesn't have '.svn' substring in the path. For this purpose find command is used with 2 conditions:

  1. -not -path "*/.svn*" filters out all svn files
  2. -not -type d filters out all directories

This will exclude empty directories from the jar though.

魂牵梦绕锁你心扉 2024-10-17 09:12:13

这可能是解决您的问题的实用方法:它不会排除 subversion 目录,而仅包含类文件(假设您将类文件放在版本控制):

jar -cf test.jar *.class

进一步增强:将代码与类文件分开。无论如何,您不想签入类文件(构建工件):

workingcopy
--src
  --.svn
  -- com  // only *.java files in here and below
--.svn
--WebContent
--bin
  --com   // only *.class files in here and below

This could be a practical workaround for your problem: it will not exclude the subversion directory (directories) but include only class files (assuming, you don't put class files under version control):

jar -cf test.jar *.class

Further enhancement: separate code from class files. You don't want to check in class files (build artefacts) anyway:

workingcopy
--src
  --.svn
  -- com  // only *.java files in here and below
--.svn
--WebContent
--bin
  --com   // only *.class files in here and below
榕城若虚 2024-10-17 09:12:13

SVN中有一个概念叫Export。如果您使用它,您将获得没有 .svn 存储库的结构。与退房时略有不同。

此后,您必须能够从该目录而不是存储库文件夹执行常规 jar 命令。

There is a concept called Export in SVN. if you use that you will get the structure without the .svn repository. It is slightly different from the Check-Out.

After this you must be able to do a regular jar command from that directory instead of the repository folder.

叹梦 2024-10-17 09:12:13

正如 javamonkey79 所说,我不认为你可以做到这一点使用 jar 工具本身。您应该做的是考虑 ant 及其 jar 任务

As said by javamonkey79, I don' tthink you can do this just with the jar tool itself. What you should do is considering ant, and its jar task.

給妳壹絲溫柔 2024-10-17 09:12:13

通过诸如 mavenantgradle

This is easily achieved (along with many other things) by build tools like maven and ant and gradle

从 Java 11 开始,您可以使用感叹号 (!) 来排除文件或文件夹,并用竖线 (|) 分隔
示例:

jar cvf MyJarFile.jar !(file1)


jar cvf MyJarFile.jar !(file1|folder1)

在 Java 8 jar 上,使用来自 @silnijlos 的 find 查看很好的答案。

您还可以使用 maven、ant 或 gradle(如果有),maven 参考:

http://maven.apache.org/plugins/maven-war-plugin/examples/include -排除-war.html中的文件

From Java 11 you can use exlamation mark (!) to exclude files or folders, seperated by pipe (|)
Examples:

jar cvf MyJarFile.jar !(file1)


jar cvf MyJarFile.jar !(file1|folder1)

On Java 8 jar, see the great answer using find from @silnijlos

You can also use maven, ant or gradle if you have that available, reference for maven:

http://maven.apache.org/plugins/maven-war-plugin/examples/including-excluding-files-from-war.html

原野 2024-10-17 09:12:13

看起来 jar 程序本身无法做到这一点。您将需要添加一些其他类型的脚本来完成您正在寻找的内容。

看来您可能使用的是 Windows,所以我猜测您可以用这种方式写入缺失的部分。

希望这有帮助。

It doesn't look like the jar program by itself will be able to do this. You will need to add in some other type of script to accomplish what you are looking for.

It looks like you may be using windows, so my guess is that you could write in this missing pieces that way.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文