简体   繁体   English

如何使用命令行构建java项目?

[英]How to build a java project using the command line?

I am a student and I hate not knowing how things are organized.我是一名学生,我讨厌不知道事情是如何组织的。 I'd like to be able to create a full on java project from scratch on the command line.我希望能够在命令行上从头开始创建一个完整的 Java 项目。 I'd like to be able to import jars and set the classpath, make packages and import them.我希望能够导入 jars 并设置类路径,制作包并导入它们。 ALso learn about environment variables.还要了解环境变量。 I currently do not know much about organizing code.我目前对组织代码知之甚少。 I just know how to code in Java.我只知道如何用 Java 编码。

Is there a textbook, online article or the like that allows one to learn how to organize a java project?是否有教科书、在线文章等可以让人们学习如何组织 Java 项目?

I do not want any involvement with eclipse or any IDE.我不想参与 eclipse 或任何 IDE。 I am willing to learn Maven, XML, or the likes to accomplish my goal.我愿意学习 Maven、XML 或类似的东西来实现我的目标。

If you are a student willing to have a Java programming career, it might help to learn how to do things from command line, eg edit the files, compiling the classes, testing and building the project.如果您是一名愿意从事 Java 编程职业的学生,​​那么学习如何从命令行进行操作可能会有所帮助,例如编辑文件、编译类、测试和构建项目。 Oracle tutorials provide example on this matter: https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html#win32-2 Oracle 教程提供了这方面的示例: https : //docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html#win32-2

However, I strongly advise you to embrace an IDE as your Java career will mostly reside in an IDE as real life projects are BIG!但是,我强烈建议您接受 IDE,因为您的 Java 职业将主要在 IDE 中进行,因为现实生活中的项目很大! There are tons of helpful things the IDE does to you out of the box or to simplify things. IDE 为您提供了大量开箱即用的有用功能或用于简化操作。 Since you are a student, I will give you one basic example besides compiling: a class with 10 fields requires you some typing for getters, setters, hashCode, equals.既然你是学生,除了编译之外,我会给你一个基本的例子:一个有 10 个字段的类需要你输入一些 getter、setter、hashCode、equals。 Alternative?选择? Few keystrokes to instruct the IDE to generate them for you.只需几次按键即可指示 IDE 为您生成它们。 And that's one basic example.这是一个基本的例子。

Regarding the project structure, embracing the (since you mentioned it) Maven project structure of src/main/{java,resources}, src/test/{java,resources} even if you do NOT use Maven.关于项目结构,即使您不使用 Maven src/main/{java,resources}, src/test/{java,resources}也要采用src/main/{java,resources}, src/test/{java,resources}的(因为您提到它)Maven 项目结构。 This will let you forget about organizing the files around.这会让您忘记组织文件。

If you were asking about structuring the classes in the right packages, you will figure out yourself as you gain experience.如果您问的是如何在正确的包中构建类,您会在获得经验时弄清楚自己。 Rule of thumb is to group classes together by functionality they provide.经验法则是根据类提供的功能将它们组合在一起。 Additionally, if the packages are organized right, if you change something and touching a few classes, ideally you'd want the changed classes to be located in a single package if possible.此外,如果包的组织正确,如果您更改某些内容并触及一些类,那么理想情况下,如果可能,您希望更改的类位于单个包中。

Learning Maven is a good choice as it is a powerful tool for building a project and keeping things organized (project structure, project dependencies, etc.).学习 Maven 是一个不错的选择,因为它是构建项目和保持组织有序(项目结构、项目依赖等)的强大工具。

A simple Java program can be compiled trivially by javac MyMainClass.java , provided that your CLASSPATH list directories and jars with its dependencies.一个简单的 Java 程序可以通过javac MyMainClass.java编译,前提是您的CLASSPATH列出了目录和 jars 及其依赖项。

Compiling a large Java project is not trivial.编译一个大的Java项目是平凡的。 There are several tools intended to make it simpler.有几种工具旨在使其更简单。

  • Gradle : very widely used, uses its own language, very powerful and complex. Gradle :使用非常广泛,使用自己的语言,非常强大和复杂。
  • Maven : Still widely used. Maven :仍然被广泛使用。 Uses XML to describe everything.使用 XML 来描述一切。
  • Apache Ant is lower level and lower abstraction power. Apache Ant级别较低,抽象能力较低。

The power of these tools lies exactly in hiding the boilerplate of the Java project building process.这些工具的强大之处在于隐藏了 Java 项目构建过程的样板文件。 They generate a skeleton of a build for you, and provide higher-level operations.它们为您生成构建的骨架,并提供更高级别的操作。

Of course you can start with simplest and watch the steps these tools make.当然,您可以从最简单的开始,然后观察这些工具的步骤。

Reading the docs for javac and jar thoroughly does help, too.彻底阅读javacjar的文档也有帮助。

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

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