Project dependencies and Maven

Figure 269. Newton's letter to Robert Hooke Slide presentation Create comment in forum
Newton's letter to Robert Hooke

Figure 270. Project »lottery« depending on »helper« Slide presentation Create comment in forum
Project »lottery« depending on »helper«

Figure 271. Providing project »helper« Slide presentation Create comment in forum
Helper.java pom.xml
package
de.hdm_stuttgart.de.mi.sd1;
public class Helper {
  static public long
    factorial(int n) {
    long result = 1;
    for (int i = 2;
        i <= n; i++) {
      result *= i;
    }
    return result;
  }
}
<project xmlns="http://maven.apache.org...>
 <modelVersion>4.0.0</modelVersion>

 <groupId>de.hdm_stuttgart.de.mi.sd1</groupId>
 <artifactId>helper</artifactId>
 <version>0.9</version>
 <packaging>jar</packaging>
 <name>helper</name>
...
</project>

Figure 272. Install project »Helper« Slide presentation Create comment in forum
goik@goiki Helper> mvn install
[INFO] Scanning for projects...
[INFO] -------------------------------------------
[INFO] Building helper 0.9
..
--------------------------------------------------
 T E S T S
--------------------------------------------------
Running de.hdm_stuttgart.de.mi.sd1.HelperTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0,
...
[INFO] Installing .../Helper/target/helper-0.9.jar to
  /ma/goik/.m2/repository/de/hdm_stuttgart/de/mi/sd1/helper/0.9/helper-0.9.jar

Figure 273. helper-0.9.jar archive content Slide presentation Create comment in forum
goik@goiki tmp> unzip  ...hdm_stuttgart/de/mi/sd1/helper/0.9/helper-0.9.jar
Archive:  .../.m2/repository/.../sd1/helper/0.9/helper-0.9.jar
   creating: META-INF/
  inflating: META-INF/MANIFEST.MF
   creating: de/
   creating: de/hdm_stuttgart/
   creating: de/hdm_stuttgart/de/
   creating: de/hdm_stuttgart/de/mi/
   creating: de/hdm_stuttgart/de/mi/sd1/
  inflating: de/hdm_stuttgart/de/mi/sd1/Helper.class
   creating: META-INF/maven/
   creating: META-INF/maven/de.hdm_stuttgart.de.mi.sd1/
   creating: META-INF/maven/de.hdm_stuttgart.de.mi.sd1/helper/
  inflating: META-INF/maven/de.hdm_stuttgart.de.mi.sd1/helper/pom.xml
  inflating: META-INF/maven/de.hdm_stuttgart.de.mi.sd1/helper/pom.properties

Figure 274. Consuming project »Lottery« Slide presentation Create comment in forum
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  ...
  <groupId>de.hdm_stuttgart.mi.sd1</groupId>
  <artifactId>lottery</artifactId>
  <version>0.9</version>
  <packaging>jar</packaging>
  <name>lottery</name>
  <dependencies>
    <dependency>
      <groupId>de.hdm_stuttgart.de.mi.sd1</groupId>
      <artifactId>helper</artifactId>
      <version>0.9</version>
    </dependency>
...
</project>

Figure 275. External libraries view Slide presentation Create comment in forum
External libraries view

Figure 276. Using Helper.factorial(...) Slide presentation Create comment in forum
static public long binomial(int n, int k) {
  return (Helper.factorial(n) / Helper.factorial(k)
                              / Helper.factorial(n - k));
}

public static void main(String[] args) {
  System.out.println("There are " + binomial(5, 2) +
      " ways to draw 2 out of 5 numbers");

  System.out.println("There are " + binomial(49, 6) +
      " ways to draw 6 out of 49 numbers");
}

Figure 277. Maven repositories Slide presentation Create comment in forum

exercise No. 102

Dealing with local Maven dependencies Create comment in forum

Q:

This exercise is actually a preparation for ongoing exercises relying on importing local Maven artifacts.

Follow this section's description and create two projects A and B. Project B shall import some implementation from project A as being previously described. If you lack an idea you may just use the given Lottery consuming Helper.