简体   繁体   English

是否可以在.jar文件中自动创建目录层次结构?

[英]Is it possible to auto create directory hierarchy in .jar file?

I'm rarely using java, so my knowledge of modern ecosystem is very limited :(. AFAIK, Java is heavy glued with directory hierarchy and file names: for example, if i have a .jar file with "com.mycompany.myapp.WndMain" class, the corresponding .class file must be named exactly "WndMain.class" (case sensitive!) and be placed into "com/mycompany/myapp" directory tree inside .jar file (that is actually a .zip archive). 我很少使用Java,所以我对现代生态系统的知识非常有限:(。AFAIK,Java与目录层次结构和文件名紧密结合:例如,如果我有一个带有“ com.mycompany.myapp.jar”的文件。 WndMain”类,则相应的.class文件必须完全命名为“ WndMain.class”(区分大小写!),并放在.jar文件(实际上是.zip存档)中的“ com / mycompany / myapp”目录树中。

But while developing applications i don't want my source code files to be named CamelCase and scattered in some directory trees! 但是在开发应用程序时,我不想将我的源代码文件命名为CamelCase并散布在某些目录树中! It's my source code, after all. 毕竟,这是我的源代码。 I prefer files to be organized the way it's comfortably for me to work with them, not for some stupid compiler and archiver :). 我更喜欢的文件组织它的舒适, 要和他们一起工作,而不是为一些愚蠢的编译器和归档:)的方式。 I want a clean "src" folder with files like "wnd_main.java", "wnd_about.java" etc. Is it some easy way to automatically generate correct file names and directory structure during compilation, based on actual source code content? 我想要一个干净的“ src”文件夹,其中包含“ wnd_main.java”,“ wnd_about.java”等文件。是否有一些简单的方法可以根据实际的源代码内容在编译过程中自动生成正确的文件名和目录结构?

Of course i can create script of my own - via Python, Ruby, Shell or whatever - that will read content of ".java" files, parse "using" and "class" directives, create directory structure, copy files, compile etc... But maybe all of this is already done years ago and all i need is something like "sudo apt-get install javamake"? 当然,我可以创建自己的脚本(通过Python,Ruby,Shell或其他工具),该脚本将读取“ .java”文件的内容,解析“ using”和“ class”指令,创建目录结构,复制文件,编译等。 ..但是,也许所有这些都已经在几年前完成了,而我需要的只是诸如“ sudo apt-get install javamake”之类的东西? :) :)

No, the compiler will not let you declare classes where the declared package name in the file (like package com.mycopmany.myapp ) do not match the actual directory structure on disk. 不,编译器不允许您声明类,这些类的文件中声明的包名称(例如package com.mycopmany.myapp )与磁盘上的实际目录结构不匹配。

This seems like a lot of work to go through to avoid having directories in your src hierarchy. 避免在src层次结构中包含目录似乎需要完成很多工作。

It will be a difficult task, as the package name inside each source file MUST be equal to the directory structure as @mattb said, but you can use some build tool like Ant (the most used one) to create the solution that you are looking for. 这将是一项艰巨的任务,因为每个源文件中的包名称必须与@mattb所说的目录结构相同,但是您可以使用一些构建工具(例如Ant) (最常用的工具)来创建您要查找的解决方案对于。

For example, if you have your source organized this way: 例如,如果您以这种方式组织了源:

src
    foo
        class_1.java
        class_2.java
    bar
        class_3.java
        class_4.java

You can use Ant to copy all source files to a temp directory, compile and package then in a .jar, but as I said, you will have some problems, like name colisions (example: you can have classes with the same name in different directoties). 您可以使用Ant将所有源文件复制到temp目录,然后在.jar中进行编译和打包,但是正如我所说的,您会遇到一些问题,例如名称colisions(例如:您可以使用不同名称的同名类权限)。 All your classes must have different names and all classes must access each other as they are in the same package. 您所有的类都必须具有不同的名称,并且所有类都必须彼此访问,因为它们在同一包中。 All this problem is related to the solution that I propose. 所有这些问题都与我提出的解决方案有关。 You can, off course, modify the code of the files that were copied to the temp directory, but I really don't think that all this is worth of try... I sinceraly recommend that you follow the "way to develop" in Java, using some IDE, following the Java Code Conventions , etc. 当然,您可以修改复制到temp目录中的文件的代码,但是我真的不认为所有这些都值得尝试...我真诚地建议您遵循以下“开发方式” Java,使用某些IDE,遵循Java代码约定等。

This is pointless. 这是没有意义的。 It's hard to find a question here. 在这里很难找到问题。
But I'll try to answer: 但我会尝试回答:

  1. Java requires your files to be strictly organized acordingly to its classnames. Java要求您的文件必须严格按照其类名进行组织
    Think of Python, which enforces indentation. 想想Python,它可以执行缩进。 It does this to eliminate the need 这样做是为了消除需求
    of coding conventions related to indentation. 与缩进有关的编码约定。
  2. It does this for your own good , to prevent situations of type: 这样做是为了自己的利益 ,以防止出现以下情况:

    How do I find this class in such a big project? 我如何在这么大的项目中找到这个班?
    It turns out I was writing a class / feature that is already present. 原来我正在写一个已经存在的类/功能。
    For this project, how should I organize the files? 对于这个项目, 应该如何组织文件? Is there some convention? 有约定吗?
    What?! 什么?! 100 classes defined in a single file? 在一个文件中定义100个类?

  3. Don't like to work with the files and directory structure manually? 不喜欢手动处理文件和目录结构吗?
    Use an IDE , it will do that for you. 使用IDE ,它将为您完成此任务。

  4. If you still want flexibility on Java, why not try some Java-based languages ? 如果仍然需要Java的灵活性,为什么不尝试一些基于Java的语言呢?
    Like Groovy, for example. 例如,像Groovy。 Or Clojure, or Scala, or... 或Clojure或Scala或...
    They all work in the JVM, their syntax is compatible with Java's, but more relaxed and permissive . 它们都在JVM中工作,其语法与Java兼容,但是更加宽松宽松 The same applies to the directory structure. 目录结构也是如此。 Bonus! 奖金! You can mix Groovy scripts with Java classes. 您可以将Groovy脚本与Java类混合使用。

Bottom note: Actually, non-public classes don't need to follow this convention . 底部注释:实际上,非公共类不需要遵循此约定
If by some strange reason you decide to declare one of your classes with package access , 如果由于某种奇怪的原因而决定声明一个具有包访问权限的类,
it won't be accessed from outside your package , but you can give it the name you want. 不会从您的程序包外部访问它,但是您可以给它起所需的名称。

You don't strictly have to organize your .java files into a directory structure that matches the packages - .class files must be in such a structure but javac will accept a flat structure for .java files - however you will make your life much easier if you do so as pretty much all the higher level Java tools in the world expect you to follow this convention. 不必严格到你的组织.java文件放在这个包相匹配的目录结构- .class文件必须在这样一个结构,但javac将接受一个扁平结构.java文件-但是你会让你的生活变得更轻松如果您这样做的话,世界上所有更高级别的Java工具都希望您遵守此约定。 For example Ant relies on matching between the .java and .class directory structures to be able to tell which .class files are out of date with respect to their corresponding .java . 例如,Ant依赖于.java.class目录结构之间的匹配,以能够分辨出相对于其相应.java而言哪个.class文件已过期。 If the structures don't match it will recompile every .java file every time. 如果结构不匹配,它将每次重新编译每个.java文件。

However the name of each .java file must match the name of the public class it defines (you can have several classes in one .java file but at most one of them can be public and its name must match the file name). 但是,每个.java文件的名称必须与它定义的public class的名称匹配(您可以在一个.java文件中包含多个类,但最多只能是一个public并且其名称必须与文件名匹配)。 This is a requirement of the compiler, and it's not worth fighting against. 这是编译器的要求,不值得与之抗争。

scons build system perfectly creates directory structure according to content of .java files. scons构建系统根据.java文件的内容完美创建目录结构。 Installation is as easy as pypm -g install scons . 安装就像pypm -g install scons一样容易。

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

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