IDEA Project Structure介绍 Modules Facets Artifacts
最近学SpringBoot时,需要搭建web环境,但是SpringBoot选了web模块也不会帮你建webapp、WEB-INF这些文件夹。于是搞了半天无法正确访问localhost。后来怀疑是modules里的web模块没有设置好,但是该怎么设置呢?
查百度得到的博客五花八门,只讲操作,也不解释为什么这么操作,令人心生怀疑,不敢跟着操作。
于是决定先把IDEA的Project Structure到底是什么,有什么用了解清楚,即可知道到底配什么,怎么配。
下面先依据jetbrains
官网对IDEA Project Structure的解释以及部分博客开始介绍。
同时介绍如何打包成jar并运行。
Project
In the IntelliJ Platform, a project encapsulates all of a project’s source code, libraries, and build instructions into a single organizational unit. Everything done using the IntelliJ Platform SDK is done within the context of a project. A project defines collections referred to as modules and libraries. Depending on the logical and functional requirements for the project, you can create a single-module or a multi-module project.
在IntelliJ平台中,一个“项目”将一个项目的所有源代码、库和指令封装到一个单独的组织单元中。使用IntelliJ平台SDK完成的所有工作。项目定义了称为模块和库的集合。根据项目的逻辑和功能需求,您可以创建单模块或多模块项目。
软件开发都要依赖SDK(Software Development Kit),在java应用开发中,就是JDK。

Module
A module is a discrete unit of functionality that can be run, tested, and debugged independently. Modules include such things as source code, build scripts, unit tests, deployment descriptors, etc. In a project, each module can use a specific SDK or inherit the SDK defined at the project level (see the SDK section below in this document). A module can depend on other modules of the project.
模块是一个独立的功能单元,可以独立地运行、测试和调试。模块包括诸如源代码、构建脚本、单元测试、部署描述符等。在一个项目中,每个模块都可以使用特定的SDK或继承在项目级别定义的SDK(参见本文下面的SDK部分)。一个模块可以依赖于项目的其他模块。
一个项目可以有一个或多个模块,比如Spring,Web。这些模块都是现有的,可用的。

如果需要添加模块,可以用+号。

Library
A library is an archive of compiled code (such as JAR files) on which modules depend.
库是模型依赖的代码集合文件(比如JAR文件)
这个应该很熟悉,可以从自己选择jar包或者用maven。

Facet
A facet represents a certain configuration, specific for a particular framework/technology associated with a module. A module can have multiple facets. E.g. Spring specific configuration is stored in a Spring facet.
facet声明了每个模块使用技术的一些配置。一个模块可能有多个facet。Spring模块的配置就声明在Spring facet中。
Spring 的主类和配置文件是Spring的一些配置,但是不告诉IDEA在哪他自己是不知道在哪的,所以在facet中就写的很清楚,什么在什么地方。

同样web模块中,他需要配什么?web.xml和webapp目录。因此上面+号添加自己的web.xml在哪。下面添加webapp在哪。
至于真的在哪没关系,一般我们建立/src/main/webapp,但是要记得给web模块配好路径。不然IDEA找不到。

Artifact
An artifact is an assembly of your project assets that you put together to test, deploy or distribute your software solution or its part. Examples are a collection of compiled Java classes or a Java application packaged in a Java archive, a Web application as a directory structure or a Web application archive, etc.
artifact是您放在一起测试、部署或发布您的软件解决方案或其部分的项目资产的集合。例如,已编译的Java类的集合或打包在Java归档中的Java应用程序、作为目录结构的Web应用程序等等。
artifact就是为了打包成为jar或war的一个配置声明。比如你想分享你的项目给小明,如何分享?自己压缩zip吗?java提供了专门打包的方法,就是jar。
那么如何打包成jar?该jar要打包什么进去?哪些你需要放进去哪些不要?你可以通过Project Structure | Artifacts告诉IDEA。就这么简单。
默认会生成对web exploded的artifact,里面的输出结构指明了项目编译文件该存哪,web资源该复制到哪,lib里要复制什么。
注意,这个lib可以自己选择添加,右边的lib上右键,添加进去就会一并输出了。

jar:Java ARchive,通常用于聚合大量的Java类文件、相关的元数据和资源(文本、图片等)文件到一个文件,以便分发Java平台应用软件或库;
war:Web application ARchive,一种JAR文件,其中包含用来分发的JSP、Java Servlet、Java类、XML文件、标签库、静态网页(HTML和相关文件),以及构成Web应用程序的其他资源;
exploded:在这里你可以理解为展开,不压缩的意思。也就是war、jar等产出物没压缩前的目录结构。建议在开发的时候使用这种模式,便于修改了文件的效果立刻显现出来。
打包成jar包
刚才讲artifact用于指明如何打包成jar包(或别的),那么现在来试着打包一下。
+号,jar,从模型中添加

寻找你的主类main class,然后选择对lib中的jar依赖怎么做。第一个选择项是不复制jar依赖,只保留链接关系;第二个是把jar依赖复制到输出目录,和你要打包的jar一起。一般选第二个,这样才能运行你的jar。
第三个配置文件的输出目录,可以不改变。 这些配置文件包括了你的jar的main class是什么啊等等。

建立完点ok

bulid artifact
你配好了如何建立artifact,那么开始build吧。

选择jar的build:

在out下就可以找到你刚刚打包的jar。

当然我们选择的是复制依赖到输出目录,因此有很多jar,包括你的example.jar:

运行jar
在Run/Bug configuartions里,添加Jar Application

配置JAR Application。告诉他你的jar在哪,并配置自动build。

好了,点ok,然后点绿色三角即可运行,效果和你平时运行应该是一样的。


参考:
https://www.cnblogs.com/deng-cc/p/6416332.html
https://www.jetbrains.com/help/idea/working-with-artifacts.html#
https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html
https://www.cnblogs.com/stm32stm32/p/9973325.html
作者:茶酒qqq
链接:https://www.jianshu.com/p/943e376cffa6
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)