Sunday, November 23, 2014

Adding an external Jar to a Maven Project (JFugue)

In this article, we show how to manually download an external jar file and install it in your local Maven repository. It is possible to automatically download from a third party central Maven repository or from your own central Maven repository. We will use JFugue 4 to demonstrate. I googled "java program to play a middle c", clicked JFugue - Java Music Programming API, clicked www.jfugue.org, clicked Download, clicked jfugue-4.0.3.jar. I googled "JFugue 4.0.3 Maven Dependency" to find the following information about JFugue:
<dependency>
   <groupId>org</groupId>
   <artifactId>jfugue</artifactId>
   <version>4.0.3</version>
</dependency>
  1. Download JFugue 4.0.3 to your Desktop.
  2. Install the jar into your local Maven repository from the Windows Command Prompt (see maven.apache.org):
    "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\jfugue-4.0.3.jar" -Dpackaging=jar -DgroupId=org -DartifactId=jfugue -Dversion=4.0.3
  3. Add the dependency xml above to your project's pom.xml file (ie "C:\Users\Scott\workspace\myrepo\my-app\pom.xml"):
    ...
    <dependencies>
       <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
       </dependency>
       <dependency>
          <groupId>org</groupId>
          <artifactId>jfugue</artifactId>
          <version>4.0.3</version>
       </dependency>
    </dependencies>
    ...
  4. Recompile the new pom.xml file:
    cd "C:\Users\Scott\workspace\myrepo\my-app"
    mvn clean compile eclipse:eclipse
  5. In Eclipse, Project Explorer, right click on my-app->Refresh (F5).
  6. Add new code to your main function:
     public static void main( String[] args )
        {
            System.out.println( "Hello World!" );
            Player player = new Player();
            Pattern pattern = new Pattern("C D E F G A B");
            player.play(pattern);
            System.exit(0);
        }
  7. Run As-Java Application.
You can now add several different types of jars and reuse lots of existing code!

This post was reposted from http://scottizu.wordpress.com/2013/08/18/adding-an-external-jar-to-a-maven-project-jfugue/, originally written on August 18th, 2013.

No comments:

Post a Comment