maven3 下jdk1.7 出现的问题解决

环境

1
2
3
4
5
6
7
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:3
7+08:00)
Maven home: d:\tools\apache-maven-3.3.3
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: c:\Program Files\Java\jdk1.7.0_45\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

使用maven命令行创建java项目

1
mvn archetype:generate -DgroupId=org.linfeng -DartifactId=mavendemo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  • 创建成功

    1
    2
    $ cd mavendemo && ls
    pom.xml src
  • pom.xml如下面所示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.linfeng</groupId>
    <artifactId>mavendemo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>mavendemo</name>
    <url>http://maven.apache.org</url>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    </project>
  • 执行maven命令
    mvn test
    报错:

    1
    2
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project mavendemo: Compilation failure
    [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

解决方案

  • 修改settings.xml,添加jdk1.7相关内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<profile>

<id>jdk-1.7</id>

<activation>

<activeByDefault>true</activeByDefault>

<jdk>1.7</jdk>

</activation>

<properties>

<maven.compiler.source>1.7</maven.compiler.source>

<maven.compiler.target>1.7</maven.compiler.target>

<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>

</properties>

</profile>

不过这样会修改所有创建项目的jre环境

  • 修改pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

重新执行mvn build,成功!

问题分析

Maven官方文档有如下描述:

编译器插件用来编译项目的源文件.从3.0版本开始, 用来编译Java源文件的默认编译器是javax.tools.JavaCompiler (如果你是用的是java 1.6) . 如果你想强制性的让插件使用javac,你必须配置插件选项 forceJavacCompilerUse.

同时需要注意的是目前source选项和target 选项的默认设置都是1.5, 与运行Maven时的JDK版本无关.如果你想要改变这些默认设置, 可以参考 Setting the -source and -target of the Java Compiler中的描述来设置 source 和target 选项.

参考资料

如何搭建docker私服

环境准备

软件包:

  • centos6.5_x86_64

  • docker-engine-1.7.0-1.el6.x86_64.rpm

docker环境搭建,请参照官方说明,本文采用的是官方的rpm包

何谓私服

官方的image镜像站位dockerhub,因为伟大的墙的缘故,所以下载镜像是很痛苦的一件事。当然你可以采用其他科学上网或者镜像加速的方法来获取image。
docker官方也提供了一个私服镜像,大家可以通过docker search registry来查找该镜像。

1
2
3
4
5
NAME                                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
registry Containerized docker registry 320 [OK]
atcol/docker-registry-ui A web UI for easy private/local Docker Reg... 55 [OK]
konradkleine/docker-registry-frontend Browse and modify your Docker registry in ... 40 [OK]
samalba/docker-registry 35

下载官方registry镜像

  • 下载镜像

使用命令docker pull registry执行下载镜像。

  • 查看镜像

下载完毕后,通过docker images 查看该镜像。

  • 给镜像打标签

执行这个命令
docker tag registry:latest localhost:5000/registry:latest

启动镜像

docker run -d -e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry -v /opt/data/registry:/tmp/registry -p 5000:5000 registry

这里有几个参数说明下:

  • 1.-e STORAGE_PATH=/tmp/registry :强制使用存储路径
  • 2.-v /opt/data/registry:/tmp/registry :绑定本地镜像存储路径
  • 3.-p 5000:5000:映射容器5000端口到本地5000端口

查看镜像状态

docker ps

查看私服状态

curl http://localhost:5000

显示如下信息,证明registry启动成功:

"\"docker-registry server\""

推送本地镜像库到registry私服

1. 第一步 给本地镜像 打tag

例如给官方的nginx镜像打tag,执行下面的命令行
docker pull nginx
docker tag nginx:latest localhost:5000/nginx:latest
查看镜像库,发现localhost:5000/nginx的镜像已经有了。

2. 第二步 推送tag到registry私服

docker push localhost:5000/nginx:latest

3. 第三步 查看私服镜像列表

curl http://localhost:5000/V1/search

看到类似这样的信息

1
2

{"num_results": 5, "query": "", "results": [{"description": null, "name": "correl/erlang"}, {"description": null, "name": "linfeng/cmd"}, {"description": null, "name": "library/my_nodejs_image"}, {"description": null, "name": "library/centos"}, {"description": "", "name": "library/nginx"}]}

拉取私服镜像

docker pull 192.168.20.85:5000/library/centos:7

结论

这只是演示如何搭建一个简单的registry私服。因为只有通过命令行方式才能查看私服信息,所以不是很便于操作。下面的博文将演示如何给registry添加web界面。

sonar环境搭建简明手册

1.sonar简介

SONAR是一个开源代码质量检查平台,根据官方的说明

sonar用于提高软件质量,提高你的开发团队效率,采用持续检查的方法……
支持c/c++,java,php,ruby,javascript,python,object c…等众多语言

2.安装包

获取安装包:sonarqube-4.3.3.zipsonar-runner-dist-2.4.zip

3.环境要求

1
jdk1.6+

运行时内存最少512M,分析内存2g以上(建议)。吃内存厉害!

4.安装步骤

  • jdk环境1.6+

  • 解压sonar安装包

注意路径中不能含有中文

执行
{sonar_home}/bin/windows-x86-64/StartSonar.bat

如果发现启动成功字样

访问下面的地址:http://localhost:9000

5.修改配置

首先,sonar默认的数据库是derby,在生产环境我们肯定要选择自己的数据库。还好sonar支持的数据库很多,这里我们选择mysql数据库。

  • 创建数据库
  • 创建用户并赋予权限
    启动mysql服务,命令行登陆,执行下面的脚本:

    1
    2
    3
    4
    5
    6
    CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

    CREATE USER 'sonar' IDENTIFIED BY 'sonar';
    GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
    GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
    FLUSH PRIVILEGES;
  • 修改下面的文件

`{sonar_home}/conf/sonar.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
#--------------------------------------------------------------------------------------------------
# WEB SERVER

# Binding IP address. For servers with more than one IP address, this property specifies which
# address will be used for listening on the specified ports.
# By default, ports will be used on all IP addresses associated with the server.
sonar.web.host=localhost

# Web context. When set, it must start with forward slash (for example /sonarqube).
# The default value is root context (empty value).
sonar.web.context=/sonar

# TCP port for incoming HTTP connections. Disabled when value is -1.
sonar.web.port=9000

说明:
参数1:sonar数据库的用户名密码
参数2:sonar数据库的连接字符串
参数3:配置web访问的ip地址或域名
参数4:默认上下文路径 /
第五个参数是启用的端口

重启sonar,发现数据库中已经自动创建了表,证明mysql数据库配置成功。

6.汉化

插件目录
{sonar_home}/extensions/plugins
sonar-l10n-zh-plugin-1.8.jar放到上面的路径下,重启sonar服务
打开页面,汉化成功。

7.安装配置sonar-runner

sonar-runner是一个提供对项目进行分析的命令行工具,安装和配置非常简单。

解压缩sonar-runner-dist-2.4.zip

配置环境变量
SONAR_RUNNER_HOME=解压路径
Path:%SONAR_RUNNER_HOME%\bin;

同样不能含有中文

然后在你需要进行代码检查的项目根目录下建立sonar-project.properties,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Required metadata
sonar.projectKey=my:project
sonar.projectName=kfhx
sonar.projectVersion=1.0

# Paths to source directories.
# Paths are relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Do not put the "sonar-project.properties" file in the same directory with the source code.
# (i.e. never set the "sonar.sources" property to ".")
sonar.sources=f:/bz/kfht

# The value of the property must be the key of the language.
sonar.language=c++

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# Additional parameters
sonar.my.property=value

sonar.projectKey= 是一个标识,不能重复,否则结果会覆盖
sonar.sources=需要检查的源码路径
sonar.language=语言的种类
sonar.sourceEncoding= 项目的编码方式

命令行下执行:
sonar-runner

然后就是漫长的等待,分析结束后访问:http://localhost:9000
就可以看到相关的分析结果。

centos下如何同时运行两个mysql实例

1.背景

由于使用了gitlab一键包,所以本机已经启动了一个mysql实例,端口号为:33061
现在需要启动另一个mysql实例,启动报错

运行命令service mysqld start

1
MySQL错误Another MySQL daemon already running with the same unix socket.

2.分析

因为已经有一个mysql实例启动,所用的SOCKET连接文件位于/tmp/mysql.sock,因此再启动mysqld会发现该文件已经被使用,才会报上面的错误。

3.解决方案

3.1 创建新实例的数据目录

此处我创建的实例路径为:/var/lib/mysql2
并且给该目录分配mysql用户权限 chown -R mysql /var/lib/mysql2

3.2 创建数据库

刚刚建立的目录下面,初始化数据库。

/usr/bin/mysql_install_db --user=mysql --datadir=/var/lib/mysql2

将mysql等系统数据库安装到实例准备运行的目录。

3.3 配置文件

我自己的配置文件,路径在/etc/my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[mysqld_multi]
mysqld = /usr/mysql/bin/mysqld_safe
mysqladmin = /usr/mysql/bin/mysqladmin
user = root
[mysqld]
datadir=/var/lib/mysql2
socket=/var/lib/mysql2/mysql.sock
user=mysql
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld2.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket = /var/lib/mysql2/mysql.sock

3.4 启动mysql服务

service mysqld start
哇咔咔 成功了!

3.5 查看当前mysql端口占用

1
2
3
netstat -alntp |grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27900/mysqld
tcp 0 0 127.0.0.1:33061 0.0.0.0:* LISTEN 19135/mysqld.bin

3.6 本地登陆mysql数据库

mysql -uroot -p 回车

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

4. 遇到的问题

4.1 本地无法登陆mysql

提示:

1
RROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

检查配置文件my.cnf,最后加入

1
2
[client]
socket = /var/lib/mysql2/mysql.sock

重启服务后,解决。

5. 参考博文

前言

  • nodejs是什么?

    nodejs是让javascript在服务器端运行的平台

  • 特点:
  1. 异步、非阻塞方式
    所有的函数都是异步的,这个将在下面的学习中体会到

  2. 事件回调机制

  1. 单线程

CommonJS

nodejs是参照CommonJS规范实现的,所以不得不探讨CommonJS规范。
简单说,CommonJS提供一个全局性方法require('module_name'),用于加载模块。

安装与配置

windows篇

这里不得不提到几个基本概念,请移步至这里扫盲。

nvm是一个linux下的管理nodejs版本的工具,
nvmw是windows上的移植版本。如果你只是在windows上研究nodejs,那么请用nvmw管理你的node版本。

获取nvmw

1
2
3
4
5
6
7
8
9
10
11
$ d:
$ cd git
$ git clone https://github.com/cnpm/nvmw.git
```

###设置环境变量

`set "PATH=d:\git\nvmw;%PATH%"`


###重新打开终端,输入`nvmw`

Node Version Manager for Windows

Usage:
nvmw help Show this message
nvmw install [version] [arch] Download and install a [version]
for [arch] architecture (optional)
nvmw uninstall [version] Uninstall a [version]
nvmw use [version] Modify PATH to use [version]
nvmw ls List installed versions

Example:
nvmw install v0.10.21 Install a specific version number of node.js
nvmw use v0.10.21 Use the specific version
nvmw install iojs Install the latest version of io.js
nvmw install iojs-v1.0.2 Install a specific version number of io.js
nvmw use iojs-v1.0.2 Use the specific version io.js

nvmw install v0.10.35 x86 Install a 32-bit version

1
2
3
4
5
6
7
8
9

###通过 `nvmw` 安装任意版本的 `node`

`nvmw install v0.12.2 node`


#一个httpserver例子

新建一个myServer.js,代码如下

var http = require(“http”);
http.createServer(function(request, response){
response.writeHead(200,{“Content-Type”:”text/plain”});
response.write(“Hello World”);
response.end();
}).listen(8888);

```

命令行执行node myServer.js

访问 http://localhost:8888/