How to install a Eclipse project to local repository
TweetPosted on Sunday Feb 23, 2014 at 04:51PM in Technology
Environment
- Apache Maven 3.1.1
- Eclipse Kepler SR1
- Oracle JDK7u51
- OS X 10.9.1
Why need to do it?
- Many kind of dependency resolving work can take on Maven.
- It is a better way than that take it on Eclipse or IDEs.
- It is stable and it has some efficient mechanism like local repository.
- We can just put in Jenkins job or shell scripts.
- It makes automation of some annoying works (e.g. build, deploy, etc) much easier.
- Thus, in this post, I'm going to try to resolve a dependency through local repository of Maven.
Create a project
- I'm going to install this project to local repository.
- After install, I will try to resolve a dependency to it from another project.
Create a project
- Create a maven project named “hogeproject” in Eclipse.
- Check “Create a simple project (skip archetype selection)”
Edit 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.nailedtothex</groupId> <artifactId>hogeproject</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
Make a class
package hogeproject; public class Hello { public String hello() { return "hello from hogeproject"; } }
Install the project to Maven repository
kyle-no-MacBook:hogeproject kyle$ pwd /Users/kyle/Documents/workspace/hogeproject kyle-no-MacBook:hogeproject kyle$ ls -l total 8 -rw-r--r--+ 1 kyle staff 566 2 23 17:07 pom.xml drwxr-xr-x+ 4 kyle staff 136 2 23 17:01 src drwxr-xr-x+ 7 kyle staff 238 2 23 17:12 target kyle-no-MacBook:hogeproject kyle$ mvn install Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8 [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building hogeproject 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hogeproject --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hogeproject --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hogeproject --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hogeproject --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hogeproject --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hogeproject --- [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ hogeproject --- Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (3 KB at 2.3 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (226 KB at 193.3 KB/sec) [INFO] Installing /Users/kyle/Documents/workspace/hogeproject/target/hogeproject-0.0.1-SNAPSHOT.jar to /Users/kyle/.m2/repository/org/nailedtothex/hogeproject/0.0.1-SNAPSHOT/hogeproject-0.0.1-SNAPSHOT.jar [INFO] Installing /Users/kyle/Documents/workspace/hogeproject/pom.xml to /Users/kyle/.m2/repository/org/nailedtothex/hogeproject/0.0.1-SNAPSHOT/hogeproject-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.438s [INFO] Finished at: Sun Feb 23 17:19:25 JST 2014 [INFO] Final Memory: 11M/245M [INFO] ------------------------------------------------------------------------ kyle-no-MacBook:hogeproject kyle$
- It showed that the jar file and pom.xml were placed somewhere under $HOME/.m2/repository.
Create an another project
- Create in Eclipse same way as above. Name is “higeproject”
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.nailedtothex</groupId> <artifactId>higeproject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <failOnMissingWebXml>false</failOnMissingWebXml> </properties> <dependencies> <dependency> <groupId>org.nailedtothex</groupId> <artifactId>hogeproject</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
- A dependency to hogeproject is specified.
Build the package by Maven
kyle-no-MacBook:higeproject kyle$ ls -l total 8 -rw-r--r--+ 1 kyle staff 825 2 23 17:27 pom.xml drwxr-xr-x+ 4 kyle staff 136 2 23 17:23 src drwxr-xr-x+ 9 kyle staff 306 2 23 17:27 target kyle-no-MacBook:higeproject kyle$ pwd /Users/kyle/Documents/workspace/higeproject kyle-no-MacBook:higeproject kyle$ mvn clean package Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8 [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building higeproject 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ higeproject --- [INFO] Deleting /Users/kyle/Documents/workspace/higeproject/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ higeproject --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ higeproject --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ higeproject --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ higeproject --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ higeproject --- [INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ higeproject --- [INFO] Packaging webapp [INFO] Assembling webapp [higeproject] in [/Users/kyle/Documents/workspace/higeproject/target/higeproject-0.0.1-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [/Users/kyle/Documents/workspace/higeproject/src/main/webapp] [INFO] Webapp assembled in [23 msecs] [INFO] Building war: /Users/kyle/Documents/workspace/higeproject/target/higeproject-0.0.1-SNAPSHOT.war [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.106s [INFO] Finished at: Sun Feb 23 17:31:39 JST 2014 [INFO] Final Memory: 9M/245M [INFO] ------------------------------------------------------------------------ kyle-no-MacBook:higeproject kyle$ unzip -l target/ classes/ higeproject-0.0.1-SNAPSHOT/ higeproject-0.0.1-SNAPSHOT.war maven-archiver/ test-classes/ kyle-no-MacBook:higeproject kyle$ unzip -l target/higeproject-0.0.1-SNAPSHOT.war Archive: target/higeproject-0.0.1-SNAPSHOT.war Length Date Time Name -------- ---- ---- ---- 0 02-23-14 17:31 META-INF/ 129 02-23-14 17:31 META-INF/MANIFEST.MF 0 02-23-14 17:31 WEB-INF/ 0 02-23-14 17:31 WEB-INF/classes/ 0 02-23-14 17:31 WEB-INF/lib/ 1986 02-23-14 17:12 WEB-INF/lib/hogeproject-0.0.1-SNAPSHOT.jar 0 02-23-14 17:31 META-INF/maven/ 0 02-23-14 17:31 META-INF/maven/org.nailedtothex/ 0 02-23-14 17:31 META-INF/maven/org.nailedtothex/higeproject/ 825 02-23-14 17:27 META-INF/maven/org.nailedtothex/higeproject/pom.xml 121 02-23-14 17:31 META-INF/maven/org.nailedtothex/higeproject/pom.properties -------- ------- 3061 11 files kyle-no-MacBook:higeproject kyle$
- We can see that the WAR contains “hogeproject-0.0.1-SNAPSHOT.jar”.
References
Tags: maven