maven2管理java项目和项目依赖
我在开发项目中,遇到重用的代码比较多,特别是一些基类,工具类,每次都是拷贝过来使用,虽然也不麻烦,但是常常还是有不爽的地方,最近看到北京一个同事建立的项目有很多可以学习的地方。
把所有的基类、工具类都放置在一个叫easyapi的项目里面,它就是一个普通的JAVA项目,存放基类,输出就是一个jar包。
其他的WEB项目就可以直接导入这个JAR包来使用这些基类了。
我用MAVEN2来管理这些项目。
1、首先,建立一个叫easyapi的项目
src/java 存放JAVA的代码
src/test 存放TEST的代码
src/conf 存放配置文件,比如log4j.properties等,
以上三个目录都是在classpath下面的
POM文件pom.xml如下:
<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>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>easyapi</name>
<build>
<finalName>easyapi-1.0</finalName>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/conf</directory>
<excludes>
<exclude>**/easyapi.properties</exclude>
</excludes>
</resource>
</resources>
<testSourceDirectory>${basedir}/src/test</testSourceDirectory>
<testResources>
<testResource>
<directory>${basedir}/src/conf</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<scm>
<connection>
scm:svn:http://www.sillycat.com/repos/easyapi
</connection>
<developerConnection>
scm:svn:http://www.sillycat.com/repos/easyapi
</developerConnection>
<url>scm:svn:http://www.sillycat.com/repos/easyapi</url>
</scm>
</project>
执行mvn clean install
可以看到其中的配置
<groupId>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
install之后会在我们的MAVEN2仓库中生成如下文件:
[INFO] Installing E:\work\easyapi\target\easyapi-1.0.jar to C:\Documents and Set
tings\Administrator\.m2\repository\com\sillycat\api\easyapi\1.0\easyapi-1.0.jar
2、生成WEB项目引入以上工具类
生成WEB项目的目录结构类似
只是在pom.xml中加入了导入以上api的jar包的东东
<dependency>
<groupId>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<version>1.0</version>
</dependency>
整个pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sillycat.soa</groupId>
<artifactId>easySOA</artifactId>
<packaging>war</packaging>
<name>easySOA</name>
<version>1.0</version>
<description />
<build>
<finalName>easySOA</finalName>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
<outputDirectory>
${basedir}/WebRoot/WEB-INF/classes
</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/conf</directory>
<excludes>
<exclude>**/easySOA.properties</exclude>
</excludes>
</resource>
</resources>
<testSourceDirectory>${basedir}/src/test</testSourceDirectory>
<testOutputDirectory>
${basedir}/WebRoot/WEB-INF/classes
</testOutputDirectory>
<testResources>
<testResource>
<directory>${basedir}/src/conf</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>
${basedir}/WebRoot
</webappDirectory>
<warSourceDirectory>
${basedir}/WebRoot
</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<scm>
<connection>
scm:svn:http://www.sillycat.com/repos/easySOA
</connection>
<developerConnection>
scm:svn:http://www.sillycat.com/repos/easySOA
</developerConnection>
<url>scm:svn:http://www.sillycat.com/repos/easySOA</url>
</scm>
</project>
3、在UBUNTU上执行easySOA的mvn install报错如下:
Downloading: http://192.168.50.90:8081/artifactory/repo/com/sun/jmx/jmxri/1.2.1/jmxri-1.2.1.jar
Downloading: https://maven-repository.dev.java.net/nonav/repository/com.sun.jmx/jars/jmxri-1.2.1.jar
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
Missing:
----------
1) javax.jms:jms:jar:1.1
Try downloading the file manually from:
http://java.sun.com/products/jms/docs.html
Then, install it using the command:
mvn install:install-file -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) com.sillycat.soa:easySOA:war:1.0
2) log4j:log4j:jar:1.2.15
3) javax.jms:jms:jar:1.1
2) com.sun.jdmk:jmxtools:jar:1.2.1
Try downloading the file manually from:
http://java.sun.com/products/JavaManagement/download.html
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun.jdmk -DartifactId=jmxtools -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=com.sun.jdmk -DartifactId=jmxtools -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) com.sillycat.soa:easySOA:war:1.0
2) log4j:log4j:jar:1.2.15
3) com.sun.jdmk:jmxtools:jar:1.2.1
3) com.sun.jmx:jmxri:jar:1.2.1
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun.jmx -DartifactId=jmxri -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=com.sun.jmx -DartifactId=jmxri -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) com.sillycat.soa:easySOA:war:1.0
2) log4j:log4j:jar:1.2.15
3) com.sun.jmx:jmxri:jar:1.2.1
----------
3 required artifacts are missing.
for artifact:
com.sillycat.soa:easySOA:war:1.0
from the specified remote repositories:
central (http://192.168.50.90:8081/artifactory/repo),
snapshots (http://192.168.50.90:8081/artifactory/repo)
正在下载这个
Downloading: http://192.168.50.90:8081/artifactory/repo/com/sun/jmx/jmxri/1.2.1/jmxri-1.2.1.jar
https://maven-repository.dev.java.net/nonav/repository/com.sun.jmx/jars/jmxri-1.2.1.jar
应该是报错找不到这个jar包
发现我的WINDOWS上是有这个东东滴,那就去导入一个到私服吧
groupId com.sun.jmx
artifactId jmxri
version 1.2.1
我在开发项目中,遇到重用的代码比较多,特别是一些基类,工具类,每次都是拷贝过来使用,虽然也不麻烦,但是常常还是有不爽的地方,最近看到北京一个同事建立的项目有很多可以学习的地方。
把所有的基类、工具类都放置在一个叫easyapi的项目里面,它就是一个普通的JAVA项目,存放基类,输出就是一个jar包。
其他的WEB项目就可以直接导入这个JAR包来使用这些基类了。
我用MAVEN2来管理这些项目。
1、首先,建立一个叫easyapi的项目
src/java 存放JAVA的代码
src/test 存放TEST的代码
src/conf 存放配置文件,比如log4j.properties等,
以上三个目录都是在classpath下面的
POM文件pom.xml如下:
<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>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>easyapi</name>
<build>
<finalName>easyapi-1.0</finalName>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/conf</directory>
<excludes>
<exclude>**/easyapi.properties</exclude>
</excludes>
</resource>
</resources>
<testSourceDirectory>${basedir}/src/test</testSourceDirectory>
<testResources>
<testResource>
<directory>${basedir}/src/conf</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<scm>
<connection>
scm:svn:http://www.sillycat.com/repos/easyapi
</connection>
<developerConnection>
scm:svn:http://www.sillycat.com/repos/easyapi
</developerConnection>
<url>scm:svn:http://www.sillycat.com/repos/easyapi</url>
</scm>
</project>
执行mvn clean install
可以看到其中的配置
<groupId>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
install之后会在我们的MAVEN2仓库中生成如下文件:
[INFO] Installing E:\work\easyapi\target\easyapi-1.0.jar to C:\Documents and Set
tings\Administrator\.m2\repository\com\sillycat\api\easyapi\1.0\easyapi-1.0.jar
2、生成WEB项目引入以上工具类
生成WEB项目的目录结构类似
只是在pom.xml中加入了导入以上api的jar包的东东
<dependency>
<groupId>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<version>1.0</version>
</dependency>
整个pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sillycat.soa</groupId>
<artifactId>easySOA</artifactId>
<packaging>war</packaging>
<name>easySOA</name>
<version>1.0</version>
<description />
<build>
<finalName>easySOA</finalName>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
<outputDirectory>
${basedir}/WebRoot/WEB-INF/classes
</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/conf</directory>
<excludes>
<exclude>**/easySOA.properties</exclude>
</excludes>
</resource>
</resources>
<testSourceDirectory>${basedir}/src/test</testSourceDirectory>
<testOutputDirectory>
${basedir}/WebRoot/WEB-INF/classes
</testOutputDirectory>
<testResources>
<testResource>
<directory>${basedir}/src/conf</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>
${basedir}/WebRoot
</webappDirectory>
<warSourceDirectory>
${basedir}/WebRoot
</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sillycat.api</groupId>
<artifactId>easyapi</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<scm>
<connection>
scm:svn:http://www.sillycat.com/repos/easySOA
</connection>
<developerConnection>
scm:svn:http://www.sillycat.com/repos/easySOA
</developerConnection>
<url>scm:svn:http://www.sillycat.com/repos/easySOA</url>
</scm>
</project>
3、在UBUNTU上执行easySOA的mvn install报错如下:
Downloading: http://192.168.50.90:8081/artifactory/repo/com/sun/jmx/jmxri/1.2.1/jmxri-1.2.1.jar
Downloading: https://maven-repository.dev.java.net/nonav/repository/com.sun.jmx/jars/jmxri-1.2.1.jar
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
Missing:
----------
1) javax.jms:jms:jar:1.1
Try downloading the file manually from:
http://java.sun.com/products/jms/docs.html
Then, install it using the command:
mvn install:install-file -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) com.sillycat.soa:easySOA:war:1.0
2) log4j:log4j:jar:1.2.15
3) javax.jms:jms:jar:1.1
2) com.sun.jdmk:jmxtools:jar:1.2.1
Try downloading the file manually from:
http://java.sun.com/products/JavaManagement/download.html
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun.jdmk -DartifactId=jmxtools -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=com.sun.jdmk -DartifactId=jmxtools -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) com.sillycat.soa:easySOA:war:1.0
2) log4j:log4j:jar:1.2.15
3) com.sun.jdmk:jmxtools:jar:1.2.1
3) com.sun.jmx:jmxri:jar:1.2.1
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun.jmx -DartifactId=jmxri -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=com.sun.jmx -DartifactId=jmxri -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) com.sillycat.soa:easySOA:war:1.0
2) log4j:log4j:jar:1.2.15
3) com.sun.jmx:jmxri:jar:1.2.1
----------
3 required artifacts are missing.
for artifact:
com.sillycat.soa:easySOA:war:1.0
from the specified remote repositories:
central (http://192.168.50.90:8081/artifactory/repo),
snapshots (http://192.168.50.90:8081/artifactory/repo)
正在下载这个
Downloading: http://192.168.50.90:8081/artifactory/repo/com/sun/jmx/jmxri/1.2.1/jmxri-1.2.1.jar
https://maven-repository.dev.java.net/nonav/repository/com.sun.jmx/jars/jmxri-1.2.1.jar
应该是报错找不到这个jar包
发现我的WINDOWS上是有这个东东滴,那就去导入一个到私服吧
groupId com.sun.jmx
artifactId jmxri
version 1.2.1