---
# System prepended metadata

title: Maven Learning Notes

---

###### tags: Maven Troubleshooting

# Maven Learning Notes
## File encoding
```
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
```

The warning can be fixed by configuring the property `project.build.sourceEncoding` in pom.xml file:
```
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
```
Check [Pom Properties](https://maven.apache.org/pom.html#properties) for more information.

## Maven Toolchain

### [ERROR] Cannot find matching toolchain definitions
```
[INFO] --- maven-toolchains-plugin:1.1:toolchain (default) @ jtcl ---
[INFO] Required toolchain: jdk [ vendor='sun' version='1.6' ]
[ERROR] No toolchain found for type jdk
[ERROR] Cannot find matching toolchain definitions for the following toolchain types:
jdk [ vendor='sun' version='1.6' ]
```
:::info
This happened when I try to run `mvn clean compile` for project [jtcl 2.9.0](https://github.com/jtcl-project/jtcl)
:::

The reason for the failure is that no toolchains.xml configured in ~/.m2/ directory.
In my environment, JDK version is 1.8. So, I need to make change for toolchains.xml and pom.xml file about JDK version.
After fixing JDK path in toolchains.xml, JDK version in pom.xml and copy the toolchains.xml file to ~/.m2 directory, it works.
```
<!-- pom.xml snippet about toolchains plugin configuration -->
<plugin>

<!-- toolchains plugin expects to find $USER/.m2/toolchains.xml -->
<!-- or use the toolchains.xml in this directory                -->
<!-- as: mvn (dash)(dash)global-toolchains `pwd`/toolchains.xml -->
<!-- be sure to edit toolchains.xml to set the path to your java 1.6 sdk -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
 <execution>
   <goals>
     <goal>toolchain</goal>
   </goals>
 </execution>
</executions>
<configuration>
<toolchains>
 <jdk>
    <version>1.8</version>
    <vendor>sun</vendor>
 </jdk>
</toolchains>
</configuration>
</plugin>

<!-- toolchains.xml -->
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>sun</vendor>
    </provides>
    <configuration>
      <jdkHome>/Program Files (x86)/java/jdk1.8.0_45</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< tcl.lang:jtcl >----------------------------
[INFO] Building jtcl 2.9.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jtcl ---
[INFO] Deleting C:\Users\egugwen\dj\jtcl\target
[INFO]
[INFO] --- maven-toolchains-plugin:1.1:toolchain (default) @ jtcl ---
[INFO] Required toolchain: jdk [ vendor='sun' version='1.8' ]
[INFO] Found matching toolchain for type jdk: JDK[/Program Files (x86)/java/jdk1.8.0_45]
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jtcl ---
[INFO] Using 'US-ASCII' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 534 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ jtcl ---
[INFO] Toolchain in compiler-plugin: JDK[/Program Files (x86)/java/jdk1.8.0_45]
[INFO] Compiling 342 source files to C:\Users\egugwen\dj\jtcl\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  14.634 s
[INFO] Finished at: 2020-09-29T08:18:22+02:00
[INFO] ------------------------------------------------------------------------
```


## Maven Archetype Usage and Problems
### Probleam 1: Archetype not found in any catalog. Falling back to central repository.

```
$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.demo -DarchetypeArtifactId=surefire-plugin-bug -DarchetypeVersion=1.4
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] Archetype not found in any catalog. Falling back to central repository.
[WARNING] Add a repository with id 'archetype' in your settings.xml if archetype's repository is elsewhere.
Downloading from lmr-cirv-nexus-kista-mirror: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/demo/surefire-plugin-bug/1.4/surefire-plugin-bug-1.4.pom
[WARNING] The POM for org.apache.maven.demo:surefire-plugin-bug:jar:1.4 is missing, no dependency information available
Downloading from lmr-cirv-nexus-kista-mirror: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/demo/surefire-plugin-bug/1.4/surefire-plugin-bug-1.4.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  54.565 s
[INFO] Finished at: 2020-09-27T19:20:05+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.0:generate (default-cli) on project standalone-pom: The desired archetype does not exist (org.apache.maven.demo:surefire-plugin-bug:1.4) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
```

:::info
It was wrong usage of `mvn archetype:generate`. The value of `archetypeGroupId, archetypeArtifactId, and archetypeVersion` must be existing archetype template on maven repository server. They are not used for the maven project to create. Those values will be asked interactively during `mvn archetype:generate` command execution.
:::

#### Right usage:
```
$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Archetype repository not defined. Using the one from [org.apache.maven.archetypes:maven-archetype-quickstart:5-SNAPSHOT -> https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public] found in catalog remote
Downloading from maven-archetype-quickstart-repo: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.pom
Downloaded from maven-archetype-quickstart-repo: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.pom (1.6 kB at 8.6 kB/s)
Downloading from maven-archetype-quickstart-repo: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/1.4/maven-archetype-bundles-1.4.pom
Downloaded from maven-archetype-quickstart-repo: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/1.4/maven-archetype-bundles-1.4.pom (4.5 kB at 40 kB/s)
Downloading from maven-archetype-quickstart-repo: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar
Downloaded from maven-archetype-quickstart-repo: https://arm001-eiffel003.rnd.ki.sw.ericsson.se:8443/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar (7.1 kB at 68 kB/s)
Define value for property 'groupId': org.apache.maven.demo
Define value for property 'artifactId': surefire-plugin-bug
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' org.apache.maven.demo: :
Confirm properties configuration:
groupId: org.apache.maven.demo
artifactId: surefire-plugin-bug
version: 1.0-SNAPSHOT
package: org.apache.maven.demo
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.apache.maven.demo
[INFO] Parameter: artifactId, Value: surefire-plugin-bug
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: org.apache.maven.demo
[INFO] Parameter: packageInPathFormat, Value: org/apache/maven/demo
[INFO] Parameter: package, Value: org.apache.maven.demo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: org.apache.maven.demo
[INFO] Parameter: artifactId, Value: surefire-plugin-bug
[INFO] Project created from Archetype in dir: C:\Users\egugwen\dj\124\surefire-plugin-bug
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  47.353 s
[INFO] Finished at: 2020-09-27T19:34:23+02:00
[INFO] ------------------------------------------------------------------------
```

## Maven Surefire Usage and Problems
### Problem 1: Could not delete temp directory

```
[WARNING] Could not delete temp directory C:\Users\egugwen\AppData\Local\Temp\surefire8601791017533718312 because Directory C:\Users\egugwen\AppData\Local\Temp\surefire8601791017533718312 u
nable to be deleted.
```

Question: how to specify the directory where the surefire plugin report is generated?
The default report directory is `${project.build.directory}/surefire-reports` and cannot be changed.

But the problem is it cannot delete temp directory, so could be caused by user privilege. When try to specify `tempDir`, it failed with exception: (this happened in Windows + Git Bash)

```
Caused by: java.lang.IllegalArgumentException: Invalid prefix or suffix
    at java.nio.file.TempFileHelper.generatePath (TempFileHelper.java:63)
    at java.nio.file.TempFileHelper.create (TempFileHelper.java:127)
    at java.nio.file.TempFileHelper.createTempDirectory (TempFileHelper.java:173)
    at java.nio.file.Files.createTempDirectory (Files.java:991)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.createSurefireBootDirectoryInTemp (AbstractSurefireMojo.java:3535)
```


## Maven Compile Usage and Problems
### Problem 1: Source option 5 is no longer supported. Use 6 or later.
This happened when JDK is 11. (JDK 8 has no this problem). To fix it, need to make change on maven-compile plugin configuraiton or use [Maven Compiler 3.8.0+](https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source).
> Since 3.8.0 the default value has changed from 1.5 to 1.6 (source and target)
```
wenijinew@localhost (testng-training/basic)  > mvn -version
Apache Maven 3.6.1 (Red Hat 3.6.1-5)
Maven home: /usr/share/maven
Java version: 11.0.8, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.8.10-2.fc32.x86_64
Default locale: sv_SE, platform encoding: UTF-8
OS name: "linux", version: "5.7.14-200.fc32.x86_64", arch: "amd64", family: "unix"
wenijinew@localhost (testng-training/basic)  > mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.testng.training:testng-training >-----------------
[INFO] Building testng-training 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testng-training ---
[INFO] Deleting /repo/wenijinew/dj/testng-training/basic/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testng-training ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /repo/wenijinew/dj/testng-training/basic/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testng-training ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /repo/wenijinew/dj/testng-training/basic/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.590 s
[INFO] Finished at: 2020-09-27T21:42:42+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project testng-training: Compilation failure: Compilation failure:
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
```

```
$ mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T21:00:29+02:00)
Maven home: C:\Users\egugwen\dj\apache-maven-3.6.1
Java version: 1.8.0_45, vendor: Oracle Corporation, runtime: C:\Program Files (x86)\Java\jdk1.8.0_45\jre
Default locale: sv_SE, platform encoding: GBK
OS name: "windows 8.1", version: "6.3", arch: "x86", family: "windows"
egugwen@SE-00006414 MINGW64 ~/dj/testng-training/basic (master)
$ mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.testng.training:testng-training >-----------------
[INFO] Building testng-training 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testng-training ---
[INFO] Deleting C:\Users\egugwen\dj\testng-training\basic\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testng-training ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\egugwen\dj\testng-training\basic\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testng-training ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Users\egugwen\dj\testng-training\basic\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.421 s
[INFO] Finished at: 2020-09-27T21:59:16+02:00
[INFO] ------------------------------------------------------------------------
```

#### Solution
* Use maven-compiler-plugin 3.1 and change configuration to set ```source``` and ```target``` as 1.6
```
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

wenijinew@localhost (testng-training/basic)  > mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.testng.training:testng-training >-----------------
[INFO] Building testng-training 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testng-training ---
[INFO] Deleting /repo/wenijinew/dj/testng-training/basic/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testng-training ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /repo/wenijinew/dj/testng-training/basic/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testng-training ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /repo/wenijinew/dj/testng-training/basic/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.456 s
[INFO] Finished at: 2020-09-27T22:08:01+02:00
[INFO] ------------------------------------------------------------------------
```

* Use maven-compiler-plugin 3.8.0
```
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
        </plugin>
    </plugins>
</build>

wenijinew@localhost (testng-training/basic)  > mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.testng.training:testng-training >-----------------
[INFO] Building testng-training 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ****
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testng-training ---
[INFO] Deleting /repo/wenijinew/dj/testng-training/basic/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testng-training ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /repo/wenijinew/dj/testng-training/basic/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ testng-training ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /repo/wenijinew/dj/testng-training/basic/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.354 s
[INFO] Finished at: 2020-09-27T22:15:24+02:00
[INFO] ------------------------------------------------------------------------
```

### Problem 2: diamond operator is not supported in -source 1.5
```
  1849 [ERROR] COMPILATION ERROR :-
  1850 [INFO] -------------------------------------------------------------
  1851 [ERROR] /C:/Users/egugwen/dj/loocsij/src/main/java/org/loocsij/j2se/lamda/LamdaExample1.java:[26,46] diamond operator is not supported in -source 1.5
  1852   (use -source 7 or higher to enable diamond operator)
  1853 [ERROR] /C:/Users/egugwen/dj/loocsij/src/main/java/org/loocsij/j2se/lamda/LamdaExample1.java:[30,31] lambda expressions are not supported in -source 1.5
  1854   (use -source 8 or higher to enable lambda expressions)
  1855 [INFO] 2 errors-
```

#### Solution
Add customized compile plugin in pom.xml file, since default source and target might be 1.5 in old version of maven-compiler-plugin.
```
   47     <build>
   48         <plugins>
   49             <plugin>
   50                 <groupId>org.apache.maven.plugins</groupId>
   51                 <artifactId>maven-compiler-plugin</artifactId>
   52                 <version>3.8.0</version>
   53                 <configuration>
   54                     <source>1.8</source>
   55                     <target>1.8</target>
   56                 </configuration>
   57             </plugin>
   58         </plugins>
   59     </build>
```

## References

* [Maven Build Lifecycle](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html)
* [POM Reference](https://maven.apache.org/pom.html)
