Kohei Nozaki's blog 

How to install a Eclipse project to local repository


Posted 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

  1. Maven - Running Maven


How to make a Jenkins job to poll SCM periodically


Posted on Sunday Feb 23, 2014 at 11:11AM in Jenkins


Environment

  • Jenkins 1.551
  • git version 1.8.3.4 (Apple Git-47)
  • OS X 10.9.1

Why need it?

  • As I wrote in How to make git commit to trigger run a Jenkins job, Git plugin of Eclipse doesn't fire hooks.
    • Unfortunately, currently EGit not supported it, and maybe will not[1].
  • So we need an alternative way, such as make Jenkins to poll SCM periodically.
    • It seems to I can configure it easily so I just try it here.

How to configure

  1. Go to configure page of a project.
  2. Go to “Build Triggers” section.
  3. Check “Poll SCM”
  4. Enter the schedule that you want in cron-style.
    • The help that placed side of input area might be useful
    • Example for poll at every 15minutes is here:
  5. Save

See the log

  • When any change is detected at polling, we can see that message in log of Jenkins.
    • In this setup, the log file is located at /var/log/jenkins/jenkins.log
Feb 23, 2014 11:24:45 AM hudson.triggers.SCMTrigger$Runner run
情報: SCM changes detected in BuildAndTestHead. Triggering  #22
Feb 23, 2014 11:24:57 AM hudson.model.Run execute
情報: BuildAndTestHead #22 main build action completed: SUCCESS

Remarks

  • I'm not sure that CPU usage or any other resource consumption of git polling.

References

  1. Eclipse Community Forums: EGit » Enabling hooks in EGit