简体   繁体   English

如何在 Eclipse 中同时构建我的自定义 java 包和项目?

[英]How do I do in Eclipse to build my custom java package and project at the same time?

It's said that a java package may contain classes, interfaces, enums, ... but I've been seeking over the net and I still haven't found what I'm looking for ... When I see package info files they also include classes... My idea is that package defines classes, interfaces, etc. in the same file but the code of the class should be in a separate class file, shouldn't it?据说一个 java 包可能包含类、接口、枚举,......但我一直在网上寻找,但我仍然没有找到我要找的东西......当我看到包信息文件时,他们也包含类...我的想法是包在同一个文件中定义类、接口等,但类的代码应该在一个单独的类文件中,不是吗?

Should I name the same package in each class I create for Eclipse to put them all together under the same package without the need of a package info file?我是否应该在为 Eclipse 创建的每个类中命名相同的包,以便将它们放在同一个包下而不需要包信息文件? On the other hand, is really necessary that Eclipse creates a treedir for each package?另一方面,Eclipse 真的有必要为每个包创建一个 treedir 吗? Could I just define at a package file where the classes are located?我可以在类所在的包文件中定义吗? What would be the content to a package info file?包信息文件的内容是什么?

It's also said that when you're going to use a package it should be in a .jar format... is it true?也有人说当你要使用一个包时,它应该是 .jar 格式......是真的吗?

What I'm trying to do is building a common custom package for using it in several projects, but I want to have it linked instead of copied to each project so I modify the code only once... is this possible in Eclipse?我想要做的是构建一个通用的自定义包以在多个项目中使用它,但我想将它链接而不是复制到每个项目,所以我只修改一次代码......这在 Eclipse 中可能吗?

Idea 1: + a packagefile with all definitions and location of classes + a bunch of clases spread by theme in different folders + eclipse will work with a linked package file creating a package in the folder tree that contains the classes referenced in the package.想法 1:+ 包含类的所有定义和位置的包文件 + 一堆按主题分布在不同文件夹中的类 + eclipse 将使用链接的包文件在包含包中引用的类的文件夹树中创建一个包。

Idea 2: + no packagefile + each class will say which packge is in by including the package name + eclipse will work with a linked bunch of classes that references the same package, so that's what it creates in the project tree.想法 2: + 没有包文件 + 每个类将通过包含包名称来说明哪个包在其中 + eclipse 将与引用相同包的一组链接类一起工作,这就是它在项目树中创建的内容。


Update 20180904:更新 20180904:

  • I would like to store all my classes in a separate folder like ".../classes/*.java"我想将所有类存储在一个单独的文件夹中,例如“.../classes/*.java”

  • I plan to link some of those classes under a package as Eclipse environment allows, say ".../src/com.dataproc/class1.java ..."我计划在 Eclipse 环境允许的情况下其中一些类链接到一个包下,比如“.../src/com.dataproc/class1.java ...”

  • I would like to compile the package and get it ready for the project I'm developing and for other projects in a special directory, say ".../mylib/common/*.jar我想编译包并为我正在开发的项目和特殊目录中的其他项目做好准备,比如“.../mylib/common/*.jar

  • When I say "at the same time" it's because I want to work in the same environment creating the classes for my package and my project.当我说“同时”时,是因为我想在相同的环境中工作,为我的包和我的项目创建类。

As you may think, I'm still a rookie with java and Eclipse, but hey, eager to learn!正如您可能认为的那样,我仍然是 Java 和 Eclipse 的新手,但是,嘿,渴望学习! I have to say I reviewed the mentioned Oracle info about packages but I still don't have a clear idea of how to proceed.我不得不说我查看了提到的有关包的 Oracle 信息,但我仍然不清楚如何继续。 It's too basic for me so I don't understand if I have to declare the package inside every class java file or declare all the classes I want inside some package-info.java file, what I do know is that I have to use "import mypackagename.*" inside my project...这对我来说太基础了,所以我不明白我是否必须在每个类 java 文件中声明包或在某个 package-info.java 文件中声明我想要的所有类,我所知道的是我必须使用“在我的项目中导入 mypackagename.*"...

Any help or orientation is really appreciated.任何帮助或方向都非常感谢。


Update 20180926: This question has been solved from this related question更新 20180926:这个问题已经从这个相关问题得到解决

First of all I try to clarify the meaning of java package , package-info.java file and JAR file.首先我尝试澄清java packagepackage-info.java文件和JAR文件的含义。

The concept of Java package is independent from the IDE that you use for development. Java 包的概念独立于您用于开发的 IDE。 From Oracle Documentation - Creating and Using Packages : a Java package is a grouping of related types providing access protection and name space management.来自Oracle 文档 - 创建和使用包Java 包是一组提供访问保护和名称空间管理的相关类型。 Note that types refers to classes, interfaces, enumerations, and annotation types.请注意,类型是指类、接口、枚举和注释类型。

The main point is: classes in the same package can access each other's package-private and protected members .要点是:同一个包中的类可以访问彼此的包私有成员和受保护成员 This is the crucial factor that you have to consider when you have to organize the source code inside your packages.当您必须在包中组织源代码时,这是您必须考虑的关键因素。


The main purpose of package-info.java file was to create a space for annotations applied to a package, besides affording a home for package-level javadoc. package-info.java文件的主要目的是为应用于包的注释创建一个空间,除了为包级 javadoc 提供一个家。 [ Joseph D. Darcy's Oracle Weblog ] [ Joseph D. Darcy 的 Oracle 博客]


JAR stands for Java ARchive . JAR代表Java 归档 It's a file format based on the popular ZIP file format and is used for aggregating many files into one.它是一种基于流行的 ZIP 文件格式的文件格式,用于将多个文件聚合为一个。

So the JAR file format is the main standard used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution .因此JAR文件格式是用于将许多 Java 类文件和关联的元数据和资源(文本、图像等)聚合到一个文件中进行分发的主要标准。


What I'm trying to do is building a common custom package for using it in several projects, but I want to have it linked instead of copied to each project so I modify the code only once... is this possible in Eclipse?我想要做的是构建一个通用的自定义包以在多个项目中使用它,但我想将它链接而不是复制到每个项目,所以我只修改一次代码......这在 Eclipse 中可能吗?

If you want to properly do that you have to structure your projects as follow:如果你想正确地做到这一点,你必须按如下方式构建你的项目:

  • Organize the reusable components in a specific project, called A from now on.在一个特定的项目中组织可重用的组件,从现在开始称为A。 A can contain as many packages as required. A可以根据需要包含任意数量的包。 A need to be build and deployed to be available to other clients.一个需要被构建和部署,以提供给其他客户。 There are tools that can be used to help you with these requirements (the most used automation build tools for Java are Maven and Gradle ).有一些工具可用于帮助您满足这些要求(最常用的 Java自动化构建工具MavenGradle )。
  • Project A compiled source code is available in a JAR file format.项目编译源代码是一个JAR文件格式提供。
  • Other projects can reuse the functionalities provided by A , let's say B is a project that depends on A : in order to reuse the components of A the project A jar file must be available in the classpath of B .其他项目可以重用A提供的功能,假设B是一个依赖于A的项目:为了重用A的组件,项目A jar 文件必须在B的类路径中可用。
  • These requirements can easily be satisfied by your IDE under the development of your projects but when you have to distribute your final products the tools previously mentioned are the best solutions.在您的项目开发过程中,您的 IDE 可以轻松满足这些要求,但是当您必须分发最终产品时,前面提到的工具是最佳解决方案。

暂无
暂无

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

相关问题 如何在Eclipse中构建Java项目以创建外部JAR - How do I build a Java project in Eclipse, to create an external JAR 如何在 Eclipse 中向我的 Java 项目添加资源文件夹 - How do I add a resources folder to my Java project in Eclipse 如何将我的 Eclipse 项目转换为早期的 Java 版本? - How do I convert my Eclipse project to an earlier Java version? 如何在Eclipse中的项目中包含.class文件? (JAVA) - How do I include .class files in my project in Eclipse? (Java) 如何构建和分发我的Java项目? - How do I build and distribute my Java project? Eclipse Java远程调试:如何确保我在项目中获得的代码与JVM中运行的代码相同? - Eclipse Java Remote Debug: How do I ensure that the code I've got in my project is the same as the code running in the JVM? 我怎么知道 eclipse 已经构建完成我的项目(禁用自动构建)? - How do I know eclipse has already build complete my project (disabled auto build)? Eclipse,Java项目-如何添加源文件夹,其中文件夹名称是软件包名称的一部分? - Eclipse, Java project - How do I add a source folder where the folder name is part of the package name? 在Eclipse没有选项的情况下如何建一个Java项目 - How do you build a Java project in Eclipse when no options are available 如何组织我的Java Eclipse项目,以便用户只需要进行一次导入? - How do I organize my Java Eclipse project so that the user then only needs to do one import?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM