转自:https://yq.aliyun.com/articles/2597javascript
Eclipse项目中系统文件介绍php
Eclipse在新建项目的时候会自动生成一些文件。这些文件好比.project、.classpath、.settings目录下的全部文件等。这些文件是Eclipse项目的元数据,描述了一个Eclipse项目。css
一般这些文件里的内容能够经过配置具体Eclipse项目的Properties来进行修改,并且普通用户无需知道这些文件具体的用处,可是本着探究问题本质的态度,咱们不妨能够了解一下这些文件及内容具体是干什么用的。当熟悉了这些文件的内容后,每每在项目配置中能够直接查看和修改文件内容,能够起到事半功倍的效果。html
Eclipse项目根目录下一般有两个文件:.project和.classpath,.project是Eclipse项目必须有的文件,而.classpath是Java项目必须有的文件。这两个文件均是XML格式的文本文件,用普通文本编辑器便可打开。java
.project描述了一个Eclipse项目。node
<?xml version="1.0" encoding="UTF-8"?> <projectDescription> <!-- name里的内容表明项目名字,对应了Eclipse项目的名称,不是Maven的finalName --> <name>demo</name> <!-- 项目的注释 --> <comment></comment> <!-- 引用的项目的名字 --> <projects> </projects> <!-- 有序的列表,定义了一系列的构建命令(buildCommand) --> <buildSpec> <buildCommand> <!-- 项目构建命令的名字 --> <name>org.eclipse.wst.jsdt.core.javascriptValidator</name> <!-- 构建命令初始化时须要传递的参数(通常看到的都是空的) --> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.wst.common.project.facet.core.builder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.m2e.core.maven2Builder</name> <arguments> </arguments> </buildCommand> </buildSpec> <!-- 项目中用到的一些特性的列表 --> <natures> <!-- 每个特性的的名字 --> <nature>org.eclipse.jem.workbench.JavaEMFNature</nature> <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.wst.common.project.facet.core.nature</nature> <nature>org.eclipse.wst.jsdt.core.jsNature</nature> </natures> </projectDescription>
Maven项目的配置nginx
一个Maven项目要确保有以下的内容,若是没有,能够手工加上下面的BuildCommand和natures:
<projectDescription> <buildSpec> <buildCommand> <name>org.eclipse.m2e.core.maven2Builder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.m2e.core.maven2Nature</nature> </natures> </projectDescription>
禁止Javascript的正确性校验程序员
其实禁止正确性校验是一个很差的习惯,但不少人有这样的需求(唐僧:配置一下exclude路径多好啊,即能进行正确性校验又不会太影响速度),这里给出方案。删除以下的buildCommand便可,也可选择性的删除以下的nature:
<buildCommand> <name>org.eclipse.wst.jsdt.core.javascriptValidator</name> <arguments> </arguments> </buildCommand> <natures> <nature>org.eclipse.wst.jsdt.core.jsNature</nature> </natures>
把一个Java项目变为dynamic web项目web
加入以下的buildSpec、nature元素便可:
<buildSpec> <buildCommand> <name>org.eclipse.wst.common.project.facet.core.builder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature> <nature>org.eclipse.wst.common.project.facet.core.nature</nature> </natures>
.project文件能够从同类型的项目中直接拷贝,但须要修改/projectDescription/name里的项目名称。编程
.classpath描述了一个Eclipse项目。
<?xml version="1.0" encoding="UTF-8"?> <classpath> <!-- 含义:src/main/java属于源码,编译后放到target/classes目录下 --> <classpathentry kind="src" output="target/classes" path="src/main/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <attributes> <!-- 表明了配置是从POM.xml里来的,受maven管理,非maven项目能够去掉这个 --> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <!-- 这里的including表明了目录下全部.java文件才会被处理,其余文件一律忽略,不会出如今target/test-classes目录下 --> <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <!-- 这里表明使用标准的JavaSE-1.7 JDK,相比来讲若是用default和直接写当前系统中安装的JDK是不推荐的 --> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <!-- 表明了Maven中的dependencies也都放到classpath里 --> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> <!-- web工程中把依赖的jar都放到输出的webapp里/WEB-INF/lib下面 --> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> </attributes> </classpathentry> <!-- --> <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache-Tomcat v7.0"> <attributes> <attribute name="owner.project.facets" value="jst.web"/> </attributes> </classpathentry> <!-- 统一的输出为target/classes --> <classpathentry kind="output" path="target/classes"/> </classpath>
项目有test/resources或test/java目录,可是不识别为classpath
酌情加入以下的classpathentry:
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java" /> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources" />
项目是maven工程,可是构建路径貌似怎么也配置不对
Maven是约定优于配置(convention over configuration)的,可是.classpath是配置型的,通常不会出现这种状况,若是出现了,检查maven约定的类路径(好比src/main/java、org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER)中是否有以下的元素:
<attributes>
<attribute name="maven.pomderived" value="true"/> </attributes>
Maven的依赖jar文件放不到/WEB-INF/lib里
确认或加入以下的配置:
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> </attributes> </classpathentry>
在项目Properties配置界面中,具体位置参考下图:
JDK配置
.classpath文件能够从同类型的项目中直接拷贝,有些目录没有的话,注意删掉对应的classpathentry,其余基本无需修改,若是有问题再考虑去改。但若是从别人的机器里拷贝,须要关注三点:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7" />
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache-Tomcat v7.0" />
,而"Apache-Tomecat v7.0"字符串须要与Eclipse Preferences里Server/Runtime Environments里的name一列保持一致。JDK为何建议配置成Execution Environment
Eclipse项目.settings目录下的配置比较杂,各类后缀名的均可以见到,绝大多数是文本文件,格式为properties(多数以.prefs为后缀名)或XML(多数以.*、.xml为文件名)格式的为主。下面挨个讲一些典型的文件。
.jsdtscope文件定义了web项目中的源码路径,也就意味着只有web project才会有这个配置。这些源码Eclipse会进行validate(若是validate没有禁用)。这个文件在实际开发中最大的价值在于定义JS文件的例外路径,在配置界面中配置的话挨个选很烦人。
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry excluding="**/*.min.js|**/bower_components/*|**/custom/*|**/node_modules/*|**/target/**|**/vendor/*" kind="src" path="src/main/webapp"/> <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject"> <attributes> <attribute name="hide" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/> <classpathentry kind="output" path=""/> </classpath>
在.jsdtscope文件的<classpathentry kind="src" path="src/main/webapp"/>
增长excluding属性,写法见“典型内容”一节。
这一部分在Eclipse不一样版本里不同,Eclipse 4.5及之后版本能够参考下面的配置,4.4.X版本(更早的版本没有考证)只能配置到具体项目中,不能全局配置。若针对具体项目配置,配置界面在项目的properties里的以下位置:
若全局进行配置,在Window/Preferences/JavaScript/Include Path中进行配置,以下图:
.jsdtscope文件能够从同类型的项目中直接拷贝,基本无需修改。
org.eclipse.core.resources.prefs文件其实就是规定项目内的文件的编码用的。通常来讲一个项目里的文件编码须要一致,特别是文件文本内容自己没法指示文件自己编码的(比较绕,XML文件第一行能指示自身编码,CSS也有这个能力但用得很少),尽可能不要多种编码同时存在(最好在编码规范中禁止多重编码同时存在的现象发生)。
eclipse.preferences.version=1 encoding//src/main/java=UTF-8 encoding//src/main/resources=UTF-8 encoding//src/test/java=UTF-8 encoding//src/test/resources=UTF-8 encoding/<project>=UTF-8
通常正常的项目打开后,应该看到是统一的编码,若是存在例外,能够对例外的文件进行转码,而后删除这个文件中例外的那一行。
org.eclipse.core.resources.prefs文件能够从同类型的项目中直接拷贝,无需修改。
org.eclipse.jdt.core.prefs文件指定了一些Java编译的特性,好比Java版本之类的,看文件每一行的key能猜出具体的用处。
eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.source=1.7
若是项目中源文件版本不正确,编译出来的目标版本不对,那么能够在这里进行修改。界面中修改的话,能够参考:
org.eclipse.jdt.core.prefs文件能够从同类型的项目中直接拷贝,无需修改。
org.eclipse.m2e.core.prefs是一些maven相关的配置。
eclipse.preferences.version=1 activeProfiles=dev resolveWorkspaceProjects=true version=1
通常在Maven项目开发时和生产环境中配置不同,能够在pom.xml中指定不一样的profile来实现,Eclipse项目开发时指定profile的话(好比指定名叫dev的profile),就能够配置这个文件的activeProfiles属性。若是在界面中配置,在这里:
org.eclipse.m2e.core.prefs文件能够从同类型的项目中直接拷贝,无需修改。
org.eclipse.wst.common.component文件规定了项目怎么组装成一个webapp,这里能够玩不少种组装方式。
<?xml version="1.0" encoding="UTF-8"?> <project-modules id="moduleCoreId" project-version="1.5.0"> <wb-module deploy-name="inkfish-web"> <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/> <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/> <property name="context-root" value="inkfish-web"/> <property name="java-output-path" value="/inkfish-web/target/classes"/> </wb-module> </project-modules>
在某些项目中,从远程maven仓库下载zip组件(固然war组件最好,是maven自带支持的)解压并放到target,做为webapp一部分,就能够在这里修改组装webapp的方式。若是在界面中配置,在这里:
org.eclipse.wst.common.component文件不可直接拷贝,如需拷贝注意修改deploy-name、某些value等一些与项目名称相关的信息。
org.eclipse.wst.common.project.facet.core.xml指示了项目中启用那些facet及facet的版本。
<?xml version="1.0" encoding="UTF-8"?> <faceted-project> <runtime name="Apache Tomcat v8.0"/> <fixed facet="wst.jsdt.web"/> <installed facet="wst.jsdt.web" version="1.0"/> <installed facet="java" version="1.7"/> <installed facet="jst.web" version="3.1"/> </faceted-project>
界面配置在下面的位置:
在实际使用中有时候在Eclipse的facet配置了之后又恢复回去了,老是配不成功,那么就能够直接修改这个文件。常见的好比servlet版本不匹配,那么就能够修改jst.web这个facet的version,若是java版本不对,那么能够修改java这个facet的version。
org.eclipse.wst.common.project.facet.core.xml文件能够从同类型的项目中直接拷贝,基本无需修改。
使用很少,无研究,略去。
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
或者也见过
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
org.eclipse.wst.jsdt.ui.superType.container文件能够在项目间项目直接拷贝,无需修改。
使用很少,无研究,略去。
Window
或者也见过
Global
org.eclipse.wst.jsdt.ui.superType.name文件能够在项目间项目直接拷贝,无需修改。
使用很少,无研究,略去。
disabled=06target eclipse.preferences.version=1
org.eclipse.wst.validation.prefs文件能够在项目间项目直接拷贝,无需修改。
有的配置是先后关联的,不是调整一个配置文件就能完事的,好比修改web工程里的Java版本,可能须要修改好几个配置文件。
这篇文章主要介绍了Eclipse项目中常见的自动生成的文件,这些文件均可以经过界面配置来修改,若是大量项目同时修改感受界面配置比较麻烦,或者纯粹是极客类型的,能够学习这些配置文件的内容。普通程序员只须要了解有这么个东西,出了一些界面上配置失灵的状况,能够直接修改文件。