Recoba20
Recoba20

Reputation: 324

Is there a way to build all my Maven projects with just one command ?

I am really new with Maven, and I have set up 2 maven projects, one of them is my utility API, the second is Jersey API. I installed utility API as part of my local Maven repository and added it in the pom.xml file.

Everything is working fine, it's just that when I want to build/run tests I need to go to each directory where projects are and run the commands. Is there a way to run one command or reconfigure the project in a way, so that I am able to process everything just from my REST API directory?

Eg. mvn clean test ... something ... -> goes and tests both of my projects.

Once again, I am new to Maven, but also I did a research and could not find a proper useful information that could help me out. If this is one of those questions that needs to be close, could you please at least provide me with some more information before closing it ? Thank you.

Upvotes: 2

Views: 1286

Answers (5)

Arek
Arek

Reputation: 3176

You can use multimodule maven projects. You define a structure like this

/
 pom.xml (A)
 util-api
   pom.xml (B)
 jersey.api
   pom.xml (C)

In the children pom's you specify a <parent> node.

(A) pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>mygroup.id</groupId>
  <artifactId>myparentartifact</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version>
  ...
  <modules>
    <module>util-api</module>
    <module>jersey-api</module>
  </modules> 
</pom>

(B) pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <parent>
    <artifactId>myparentartifact</artifactId>
    <groupId>mygroup.id</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>util-api</artifactId>
  <packaging>jar</packaging>
</pom>

(C) pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <parent>
    <artifactId>myparentartifact</artifactId>
    <groupId>mygroup.id</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>jersey-api</artifactId>
  <packaging>jar</packaging>
</pom>

With this configuration you can build all modules with one command: mvn clean install

Upvotes: 4

Oleksii Vynnychenko
Oleksii Vynnychenko

Reputation: 320

You need to create parent POM for your 2 modules with
<packaging>pom</packaging>
so when you run for example mvn install on your parent POM it will run it on it's every child module.

More info on working with multiple modules here

Upvotes: 0

Bhargav Kumar R
Bhargav Kumar R

Reputation: 2200

There is an option to combine related projects into one by having a top level POM using modules feature.

<modules>
   <module>utility-api</module>
   <module>Jersey-api</module>
</modules>

and can compile and run the test cases with

mvn clean install

Upvotes: 0

narancs
narancs

Reputation: 5294

Are you looking for a command , which can run your test and also build you app with all the module included ? I thin you are looking for mvn clean install.

Please make sure, you say this in the highest maven pom.xml, which all include all the dependencies.

Upvotes: 0

kctang
kctang

Reputation: 11202

You can use Maven's aggregation pom.

Upvotes: 0

Related Questions