简体   繁体   English

执行jar命令排除文件

[英]Execute jar command exclude files

I followed The Java Archive Tool but couldn't find how to exclude folders.我遵循了The Java Archive Tool,但找不到如何排除文件夹。

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.我只想从src下的类编译和创建 jar,不包括以 .svn 目录开头的所有文件夹。 I want to run it from cmd like jar -cf test.jar etc..我想从cmd运行它,比如jar -cf test.jar等。

How can I do it?我该怎么做?

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. jar的问题在于,如果传递目录,它将与所有内容一起递归添加。 So our goal is to pass only files and only those of them which doesn't have '.svn' substring in the path.所以我们的目标是只传递文件,并且只传递那些在路径中没有 '.svn' 子字符串的文件。 For this purpose find command is used with 2 conditions:为此, find命令与 2 个条件一起使用:

  1. -not -path "*/.svn*" filters out all svn files -not -path "*/.svn*"过滤掉所有 svn 文件
  2. -not -type d filters out all directories -not -type d过滤掉所有目录

This will exclude empty directories from the jar though.不过,这将从 jar 中排除空目录。

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):这可能是您问题的实用解决方法:它不会排除subversion 目录(目录),而只包含类文件(假设您没有将类文件置于版本控制之下):

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

这可以通过构建工具(如mavenantgradle )轻松实现(以及许多其他事情)

There is a concept called Export in SVN. SVN中有一个叫做Export的概念。 if you use that you will get the structure without the .svn repository.如果您使用它,您将获得没有 .svn 存储库的结构。 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.在此之后,您必须能够从该目录而不是存储库文件夹执行常规 jar 命令。

As said by javamonkey79 , I don' tthink you can do this just with the jar tool itself.正如javamonkey79 所说,我认为您不能仅使用jar工具本身来做到这一点。 What you should do is considering ant , and its jar task .您应该做的是考虑ant及其jar 任务

From Java 11 you can use exlamation mark (!) to exclude files or folders, seperated by pipe (|) Examples:从 Java 11 开始,您可以使用感叹号 (!) 来排除文件或文件夹,以管道 (|) 分隔示例:

jar cvf MyJarFile.jar !(file1)


jar cvf MyJarFile.jar !(file1|folder1)

On Java 8 jar, see the great answer using find from @silnijlos在 Java 8 jar 上,使用来自 @silnijlos 的 find 查看很好的答案

You can also use maven, ant or gradle if you have that available, reference for maven:如果你有可用的 maven、ant 或 gradle,你也可以使用 maven 的参考:

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

It doesn't look like the jar program by itself will be able to do this.看起来 jar 程序本身无法做到这一点。 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.看起来您可能正在使用 Windows,所以我的猜测是您可以通过这种方式编写缺少的部分。

Hope this helps.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM