Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态。
自从2.0.9后,新增了1种,现在有了6种scope:
- compile
默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。 - provided
跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。 - 使用这个时,不会将包打入本项目中,只是依赖过来。
- 使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里
- when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
- <!-- Servlet -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.1</version>
- <scope>provided</scope>
- </dependency>
- runtime
表示dependency不作用在编译时,但会作用在运行和测试时 - test
表示dependency作用在测试时,不作用在运行时。 - system
跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 例如:
<project>
...
<dependencies>
<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
</dependencies>
...
</project>
- import (Maven 2.0.9 之后新增)
它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置,例如: This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>maven</groupId>
<artifactId>B</artifactId>
<packaging>pom</packaging>
<name>B</name>
<version>1.0</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>maven</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>d</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
B项目导入A项目中的包配置
Dependency Scope
在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前<scope>可以使用5个值:
* compile,缺省值,适用于所有阶段,会随着项目一起发布。
* provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
* runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
* test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
* system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
1、DepencyManagement应用场景
当我们的项目模块很多的时候,我们使用Maven管理项目非常方便,帮助我们管理构建、文档、报告、依赖、scms、发布、分发的方法。可以方便的编译代码、进行依赖管理、管理二进制库等等。
由于我们的模块很多,所以我们又抽象了一层,抽出一个juxin-base-parent来管理子项目的公共的依赖。为了项目的正确运行,必须让所有的子项目使用依赖项的统一版本,必须确保应用的各个项目的依赖项和版本一致,才能保证测试的和发布的是相同的结果。
在我们项目顶层的POM文件中,我们会看到dependencyManagement元素。通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。
来看看我们项目中的应用:
依赖关系:
juxin-base-parent(pom.xml)
转来的一篇博文,此文详细说明了ETAG的作用。我发现网上很多人都在询问ETAG的对于优化网站的作用,有些人直接建议关闭ETAG。这篇博文可以完整的说明ETAG的作用,是否关闭,或者如何配置,要根据自己网站的情况而定。
开始ETAG测试
首先在浏览器输入以下访问地址
比如我们访问某网站:http://xxxxxxxx/
第一次的http请求代码如下(只包含头部信息)
———————————————————-
CODE:
GET / HTTP/1.1
Host: xxxxxxxx
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; ja; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: ja,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
———————————————————-
完成类、方法、变量名称的自动输入
2. Ctrl + N(Ctrl + Shift + N)
跳转到指定的Java文件(其它文件)
3. Ctrl + B
跳转到定义处
4. Ctrl + Alt + T
用if、while、try catch来围绕选中的代码行
5. Ctrl + Alt + B
跳转到方法实现处
6. Ctrl + W
按一个word来进行选择操作,在IDEA里的这个快捷键功能是先选择光标所在字符处的单词,然后是选择源
代码的扩展区域。
7. Shift + F1
在浏览器中显示指定的Java docs
8. Ctrl + Q
在editor window中显示java docs这个功能很方便--因为有时仅仅是忘记了自己编写的方法中的某个参数的含义,此时又不想再起一个浏览器来查看java doc,此时这个功能的好处就体现出来了
9. Ctrl + /
注释/反注释指定的语句,你也可以用Ctrl + Shift + / 来进行多行语句的注释(即使用多行注释符号"/* ... */")
10. F2/Shift + F2
跳转到下/上一个错误语句处
11. Shift + F6
提供对方法、变量的重命名
sb.append(" _ooOoo_\n");
sb.append(" o8888888o\n");
sb.append(" 88\" . \"88\n");
sb.append(" (| -_- |)\n");
sb.append(" O\\ = /O\n");
sb.append(" ____/`---'\\____\n");
sb.append(" .' \\\\| |// `.\n");
sb.append(" / \\\\||| : |||// \\ \n");
sb.append(" / _||||| -:- |||||- \\ \n");
sb.append(" | | \\\\\\ - /// | |\n");
sb.append(" | \\_| ''\\---/'' | |\n");
sb.append(" \\ .-\\__ `-` ___/-. /\n");
sb.append(" ___`. .' /--.--\\ `. . __\n");
sb.append(" .\"\" '< `.___\\_<|>_/___.' >'\"\".\n");
sb.append(" | | : `- \\`.;`\\ _ /`;.`/ - ` : | |\n");
sb.append(" \\ \\ `-. \\_ __\\ /__ _/ .-` / /\n");
sb.append("======`-.____`-.___\\_____/___.-`____.-'======\n");
sb.append(" `=---='\n");
sb.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
sb.append("\t\t佛祖保佑 永不宕机 永无BUG\n");
System.err.println(sb.toString());
其实这个实现就是使用了MessageFormat这个类;
String content = "ab,cc,{名称},{密码},{日期},dd,ff";
String array[] = {userName, password, format.format(new Date())};
content = MessageFormat.format(content, array);
解释如下:
content 中需要被替换的就是{}中的参数,array数组中存放的是对应的要替换的参数;使用MessageFormat方法的时候,需要要将这些参数的个数匹配正确,并且数序要指定,否则匹配出错。这样就实现了参数的替换。很简单,也很死板。
MessageFormat:出自java.text包中;
大家可以自己打开jdk源代码读一下他的实现方式。
Java里从来少不了字符串拼接的活,Java程序员也肯定用到过StringBuffer,StringBuilder,String.format以及被编译器优化掉的+=。但这些都和下文要谈的无关。
比如有这样的字符串:
张三将去某地点找李四。
其中,张三某地点和李四都是可变的,比如张三变成王五,某地点变成纽约,李四变成赵六。于是整句变成:
王五将去纽约找赵六。
如果直接将张三,某地点和李四用变量替代,再拼接起来,可以达到目的。但是,代码不好写,也不好看,也不好维护。但是,我看过很多SQL拼接,HTML拼接都是这样做的。我自己以前也是这样,自从接触了MessageFormat.format之后才意识到有更好的形式。请看下面的代码:
String[] tdArr=...;
String result=MessageFormat.format("
这段代码将把数组tdArr中的四个元素分别插入到{0},{1},{2},{3}的位置。
你看看,是不是这样形式和内容有效的分开了。容易想象,当元素增多时,这种方式优势很明显。
一件事有很多手段来达成,知道那种手段更好,是你经验的体现和专业化的特征。
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
确认已经安装PCRE
[ubuntu@titan]# sbin/nginx
sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[ubuntu@titan]# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[ubuntu@titan]# whereis libpcre.so.1
libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so /usr/local/lib/libpcre.so.1
添加软链接:
[ubuntu@titan]# ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1
前面在一般的linux上可以解决此问题.
注: 在有的操作系统上面,安装pcre后,安装的位置为/usr/local/lib/*pcre*
在redhat 64位机器之上有这样的情况.
在redhat 64位机器上, nginx可能读取的pcre文件为/lib64/libpcre.so.1文件.
所以在改用下面的软连接:
[ubuntu@titan]# ln -s /usr/local/lib/libpcre.so.1 /lib64
废话不多说,解决方式是,在pom.xml文件中的build节点中,添加如下代码:







