Featured image of post Java Maven

Java Maven

Learning Maven

Prerequisition

add .vscode settings.json such as

1
2
3
{
    "java.configuration.updateBuildConfiguration": "automatic"
}

Create Project

There are two ways creating Maven project

Using command

we can create project using command

1
mvn archetype:generate "-Dfilter=org.apache.maven.archetypes:"

if we run the command without -Dfilter, there are thousands of files appear on the terminal (try by yourself). Pick the “quickstart” template option. In my case, this is number 9

1
9: remote -> org.apache.maven.archetypes:maven-archetype-quickstart

Then choose maven version (pick the latest version), groupID as organizatoin name or your name e.g. “com.ansuf”, artifactID is the project name e.g. “todoapp” and the last is package that we can put as the same as groupID.

Maven Command

After succeffully creating the project, cd terminal to the project directory. And run mvn compile then you’ll see a new folder target created in the project directory.

Maven Clean

To clean it up - after running mvn compile or mvn test, just run mvn clean

Using IDE (VsCode)

  • run cmd/ctrl + shift + p > choose Maven > maven-archetype-quickstart

Add Dependency

Using IDE

  • run cmd/ctrl + shift + p > choose Maven: add a dependency
  • input keywords to search artifacts from Maven Central Reposory, i.e. spring-jdbc
  • pick the package that you want

Profile

Profiles are a useful tool to set up environment‑specific build properties. They are usually created in the pom.xml, but can be configured in the settings.xml. We’re going to create an example using the maven.test.skip or the skipped property inside of your pom.xml. And the same profiles can be executed from the command line as well using the ‑D property. A great example of profiles are if you’ve used anything specific to your environment such as a database or database connection that you might use for testing and you can enter those parameters through a profile.

Creating Profile

put the profiles in the pom.xml file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
 <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/dev</directory>
          </resource>
        </resources>
      </build>
    </profile>
    <profile>
      <id>prod</id>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/prod</directory>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>
...

You can put anywhere but, I sugest to put after <dependencies> and before <build>.

create logging.properties file in the src/main/resources/prod with content logging.level=prod and also for the dev profile.

Run Profile via IDE

now you can test it using maven controller in vsCode panel. run the Lifecycle clean - compile, see the file target/classes/logging.properties and check the content. Play around by changing the Profiles to dev, compile the code and check again the result.

java maven in vscode
Maven pannel in vscode

Run Profile via CLI

you also can run or compile the profile via cli using command

1
mvn clean compile -P prod

Multi Module Project

Multi Module Project is a project that consist multiple project inside it.

Using CLI

  1. create parent module or project, run
1
2
3
4
5
mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=webproject \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false
  1. In your parent pom.xml, make sure it has:
1
<packaging>pom</packaging>

update pom.xml in the parent directory, webproject. Change the section to use modules/….

1
2
3
4
5
6
// ...existing code...
  <modules>
    <module>modules/webmodel</module>
    <module>modules/webclient</module>
  </modules>
// ...existing code...
  1. from terminal ensure you are in the webproject folder, and run

run

1
2
3
4
5
6
7
mkdir webproject #create webproject
cd webproject #go to the submodule directory
mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=my-parent \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

update pom.xml in the module directory

1
2
3
4
5
6
    <parent>
          <groupId>com.example</groupId>
          <artifactId>webproject</artifactId>
          <version>1.0-SNAPSHOT</version>
          <relativePath>../../pom.xml</relativePath>
    </parent>
  1. alternatively you can create manualy, instead of using step 3, by running
1
2
3
4
5
mkdir webmodel
cd webmodel
touch pom.xml
mkdir -p src/main/java
mkdir -p src/test/java

and put pom.xml as follow

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<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>com.example</groupId>
  <artifactId>webclient</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>webclient</name>

  <parent>
    <groupId>com.example</groupId>
    <artifactId>webproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../../pom.xml</relativePath>  <!-- points up two levels -->
  </parent>
  
</project>

Using IDE

  • create project using cmd/ctr + shift + p -> Java: Create java project -> Maven -> maven-archetype-quickstart
  • on the project explorer, right click the project -> Maven -> “New Module..”
    • select the parent,
    • enter name of the project
  • you can move the project module to directory modules but you need to update the pom file both in the parent and the module (see previous tutorial)

Reload VsCode

If you have problem with the IDE, reload the Java Language Server to ensure VS Code recognizes the configuration. Use the command palette (Cmd+Shift+P on macOS) and run:

1
Java: Clean Language Server Workspace

Then build the reactor from the parent folder so webmodel is available on the classpath: and run

1
2
cd webproject
mvn clean install

Create Module

Using BOM

Bill of materials file, also referred to as a BOM. it’s essentially a parent POM file.

There are a few things to consider as to why you might want a BOM. First off is if you’re going to be using one, you need to specify the type of POM in the BOM file. It is one of the specified types that you can have of that. It basically means it’s a parent project. The reason why you’d want to use this though is it allows you to explicitly define specified versions for your subprojects or your imported POM to use. We just sort of saw an example of this with the parent POM in the multi‑module project, but we can use this as an independent file and just utilize the dependency management portion of it.

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus