IDEAProjectStructure介绍ModulesFacetsArtifacts
最近学SpringBoot时,需要搭建web环境,但是SpringBoot选了web模块也不会帮你建webapp、WEB-INF这些⽂件夹。于是搞了半天⽆法正确访问localhost。后来怀疑是modules⾥的web模块没有设置好,但是该怎么设置呢?
查百度得到的博客五花⼋门,只讲操作,也不解释为什么这么操作,令⼈⼼⽣怀疑,不敢跟着操作。
于是决定先把IDEA的Project Structure到底是什么,有什么⽤了解清楚,即可知道到底配什么,怎么配。
下⾯先依据jetbrains官⽹对IDEA Project Structure的解释以及部分博客开始介绍。
同时介绍如何打包成jar并运⾏。
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。
image.png
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 section below in this document). A module can depend on other modules of the
project.
模块是⼀个独⽴的功能单元,可以独⽴地运⾏、测试和调试。模块包括诸如源代码、构建脚本、单元测试、部署描述符等。在⼀个项⽬中,每个模块都可以使⽤特定的SDK或继承在项⽬级别定义的SDK(参见本⽂下⾯的SDK部分)。⼀个模块可以依赖于项⽬的其他模块。
⼀个项⽬可以有⼀个或多个模块,⽐如Spring,Web。这些模块都是现有的,可⽤的。
image.png
如果需要添加模块,可以⽤+号。
image.png
A library is an archive of compiled code (such as JAR files) on which modules depend.
库是模型依赖的代码集合⽂件(⽐如JAR⽂件)
这个应该很熟悉,可以从⾃⼰选择jar包或者⽤maven。
image.png
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中就写的很清楚,什么在什么地⽅。
image.png
同样web模块中,他需要配什么?l和webapp⽬录。因此上⾯+号添加⾃⼰的l在哪。下⾯添加webapp在哪。
⾄于真的在哪没关系,⼀般我们建⽴/src/main/webapp,但是要记得给web模块配好路径。不然IDEA不到。
image.png
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上右键,添加进去就会⼀并输出了。
image.png
jar:Java ARchive,通常⽤于聚合⼤量的Java类⽂件、相关的元数据和资源(⽂本、图⽚等)⽂件到⼀个⽂件,以便分发Java 平台应⽤软件或库;
war:Web application ARchive,⼀种JAR⽂件,其中包含⽤来分发的JSP、Java Servlet、Java类、XML⽂件、标签库、静态⽹页(HTML和相关⽂件),以及构成Web应⽤程序的其他资源;
exploded:在这⾥你可以理解为展开,不压缩的意思。也就是war、jar等产出物没压缩前的⽬录结构。建议在开发的时候使⽤这种模式,便于修改了⽂件的效果⽴刻显现出来。
打包成jar包
刚才讲artifact⽤于指明如何打包成jar包(或别的),那么现在来试着打包⼀下。
1. +号,jar,从模型中添加
t
2. 寻你的主类main class,然后选择对lib中的jar依赖怎么做。第⼀个选择项是不复制jar依赖,只保留链接关系;第⼆个是把jar依赖复
制到输出⽬录,和你要打包的jar⼀起。⼀般选第⼆个,这样才能运⾏你的jar。
第三个配置⽂件的输出⽬录,可以不改变。这些配置⽂件包括了你的jar的main class是什么啊等等。
建⽴完点ok
image.png
3. bulid artifact
你配好了如何建⽴artifact,那么开始build吧。
image.png
选择jar的build:
image.png
在out下就可以到你刚刚打包的jar。
image.png
当然我们选择的是复制依赖到输出⽬录,因此有很多jar,包括你的example.jar:
idea debug
image.png
运⾏jar
在Run/Bug configuartions⾥,添加Jar Application
image.png
配置JAR Application。告诉他你的jar在哪,并配置⾃动build。
image.png
好了,点ok,然后点绿⾊三⾓即可运⾏,效果和你平时运⾏应该是⼀样的。
image.png
image.png
参考:
作者:茶酒qqq
链接:www.jianshu/p/943e376cffa6
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,⾮商业转载请注明出处。