Menu

Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

What is the difference between Maven and Gradle?

Maven and Gradle are build automation tool that help us to to build our project.

Gradle faster than Maven?
Gradle avoids unnecessary compilation, that means if the binary interface of a library has not changed from previous build then new build will not compile the file again.

Cannot find matching toolchain definitions for the following toolchain types jdk

In this case to resolve the build issue we need to create a toolchains.xml file in .m2 directory. And add the following lines into that file.


<?xml version="1.0" encoding="UTF-8"?>

<toolchains>

  <!-- JDK toolchains -->

  <toolchain>

    <type>jdk</type>

    <provides>

      <version>11</version>

      <vendor>oracle</vendor>

    </provides>

    <configuration>

      <jdkHome>C:\Program Files\Java\jdk-11.0.15</jdkHome>

    </configuration>

  </toolchain>

</toolchains>


Here you need to update the version of JDK and jdkHome path according to installed JDK version in your machine.

After updating toolchains.xml file, go back to your project and execute the command to build the project.


Skip Running Tests from Maven Build

There are multiple ways to skip the execution of the tests from a Maven project. So that at the time of build it doesn't compile the unit tests. Following are the steps using that you can skip the unit test compilation.

1. The first way is; while compiling or building the project using mvn install command. There are two commands that you can use on the command line to skip the tests running.

a. -DskipTests

b. -Dmaven.test.skip=true

2. Another way is to add the skipTests configuration in POM.xml file within the maven-surefire-plugin plugin. Below is the plugin code snippet for POM file.

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>

Reference

  1. Skipping Tests - Maven

Failed to read artifact descriptor | Maven

Problem

Failed to read artifact descriptor for com.adobe.acs:acs-aem-commons-bundle:jar:3.17.2: could not find artifact com.adobe.acs:acs-aem-commons:pom:3.17.2 in central (https://repo.maven.apache.org/maven2)

Resolution

In case you are seeing this error while building the code using Maven then please try to run the maven command with -Padobe-public or setting ssl=true. Below are the sample command lines.

mvn -PautoInstallPackage -Padobe-public clean install

OR

mvn clean install -P autoInstallPackage -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true

This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced

When we build a maven project, we might encounter a very common Maven cached error, due to that our build is getting failed. This is caused by Maven’s default update policy. With respect to fixing this error, er have to run the "mvn install" command in forcefully update mode.


To bypass this default restriction, you can do the following:
  •     add the -U flag to the maven install command, or
  •    configure your Maven clients’ settings.xml file with the following value <updatePolicy>always</updatePolicy>. Below is the sample settings.xml file.
<settings xmlns="https://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://maven.apache.org/SETTINGS/1.0.0
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
 <!-- ====================================================== -->
 <!-- A D O B E   P U B L I C   P R O F I L E                -->
 <!-- ====================================================== -->
     <profile>
         <id>adobe-public</id>
         <activation>
             <activeByDefault>true</activeByDefault>
         </activation>
         <properties>
             <releaseRepository-Id>adobe-public-releases</releaseRepository-Id>
             <releaseRepository-Name>Adobe Public Releases</releaseRepository-Name>
             <releaseRepository-URL>https://repo.adobe.com/nexus/content/groups/public</releaseRepository-URL>
         </properties>
         <repositories>
             <repository>
                 <id>adobe-public-releases</id>
                 <name>Adobe Public Repository</name>
                 <url>https://repo.adobe.com/nexus/content/groups/public</url>
                 <releases>
                     <enabled>true</enabled>
                     <updatePolicy>never</updatePolicy>
                 </releases>
                 <snapshots>
                     <enabled>false</enabled>
                 </snapshots>
             </repository>
         </repositories>
         <pluginRepositories>
             <pluginRepository>
                 <id>adobe-public-releases</id>
                 <name>Adobe Public Repository</name>
                 <url>https://repo.adobe.com/nexus/content/groups/public</url>
                 <releases>
                     <enabled>true</enabled>
                     <updatePolicy>always</updatePolicy>
                 </releases>
                 <snapshots>
                     <enabled>false</enabled>
                 </snapshots>
             </pluginRepository>
         </pluginRepositories>
     </profile>
</profiles>
 <activeProfiles>
     <activeProfile>adobe-public</activeProfile>
 </activeProfiles>
</settings>

Maven project configuration for module is not available

Build failed | computer programming guide
Maven build failed.

Maven resources compiler: Maven project configuration required for module '<project name>' isn't available. Compilation of Maven projects is supported only if external build is started from an IDE.

Solution

Right click on the POM.xml file of your project and click on the option "Add as Maven Project".

This will import your project as the Maven project and starts downloading the dependencies. 




No goals have been specified for this build

When we started a maven project without setting the run goal then the Maven build will get failed and generate the below error "No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]". 

In this case, we have to set the maven goal e.g. clean install to successfully build and run the project or application. 

Resolution

If you are running using IDE then please set the command line as mentioned below and restart the build. To set the command line with IntelliJ, please go to Run >> Edit Configurations >> Select your project >> Select Parameters tab from left >> put clean install in command line field.

Now rerun the maven build.


Error while running the Maven project.

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.882 s
[INFO] Finished at: 2021-06-09T07:28:32+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [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/NoGoalSpecifiedException

Process finished with exit code 1

Failed to collect dependencies mule-core-ee:jar:3.8.4


Few times I faced this issue, while working with Mule 3.8.4. When I Googled I found people are talking about the Mulesoft Enterprise Edition access to download these dependencies for project, but I don't think we need nexus EE access to download these dependencies specially when we are working with Mule 3. Here the problem is something different and your POM might have some additional dependencies which is not available on public nexus repository and due to that this dependency is not getting resolved.

Failed to collect dependencies at com.mulesoft.muleesb:mule-core-ee:jar:3.8.4: Failed to read artifact descriptor for com.mulesoft.muleesb:mule-core-ee:jar:3.8.4: Could not transfer artifact com.mulesoft.muleesb:mule-core-ee:pom:3.8.4 from/to Central (http://repo1.maven.org/maven2/): Failed to transfer file http://repo1.maven.org/maven2/com/mulesoft/muleesb/mule-core-ee/3.8.4/mule-core-ee-3.8.4.pom with status code 501


Resolution:

To resolve this in Mule 3 Anypoint studio, create a new fresh maven project and build it. If this newly created project successfully build then your other project where you are facing this issue will work. Match the project POM.xml file with new project pom and see the dependencies.

Example POM.xml file.

maven commands/phases

Maven Lifecycle Phases: Phases are actually mapped to underlying goals. The specific goals executed per phase is dependent upon the packaging type of the project. So when we try to build and compile our code using mvn command then along with mvn we pass certain goal as well which actually perform the task which we want to perform.
e.g. 

mvn validate

Maven validate goal is to validate the project structure and all necessary entries.

mvn compile

Maven compile is to compile the code.

mvn package

Maven package is to package the compile  project into a deploy-able or attributable file JAR. We can also package the required dependencies using the package command.

mvn install

Maven install phases is to build and deploy the project in your local machine.

mvn deploy

maven deploy is to deploy the project on remote server and repository to share the project with other team and developer. generally we do when we wants to move final release deployment from lower environment to UAT/prod or transporting the release from one environment to another.

mvn test

Test the compiled project code with written unit test cases. This will not deploy or package your project.

mvn verify

Run this phase to verify that the created package is valid and met all the criteria.

mvn integration-test

process and deploy the package of the code in the environment where integration testing need to perform.

mvn clean

Remove the old package, artifacts and take a fresh build then create a new package of project code.

mvn site

Generate site documentation for the project. We can find the generated documents under target directory.

References:

useful maven command and their usage


usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
 -am,--also-make                        If project list is specified, also build projects required by the list
 -amd,--also-make-dependents            If project list is specified, also build projects that depend on projects on the list
 -B,--batch-mode                        Run in non-interactive (batch) mode
 -b,--builder <arg>                     The id of the build strategy to use.
 -C,--strict-checksums                  Fail the build if checksums don't match
 -c,--lax-checksums                     Warn if checksums don't match
 -cpu,--check-plugin-updates            Ineffective, only kept for backward compatibility
 -D,--define <arg>                      Define a system property
 -e,--errors                            Produce execution error messages
 -emp,--encrypt-master-password <arg>   Encrypt master security password
 -ep,--encrypt-password <arg>           Encrypt server password
 -f,--file <arg>                        Force the use of an alternate POM file (or directory with pom.xml).
 -fae,--fail-at-end                     Only fail the build afterwards; allow all non-impacted builds to continue
 -ff,--fail-fast                        Stop at first failure in reactorized builds
 -fn,--fail-never                       NEVER fail the build, regardless of project result
 -gs,--global-settings <arg>            Alternate path for the global settings file
 -gt,--global-toolchains <arg>          Alternate path for the global toolchains file
 -h,--help                              Display help information
 -l,--log-file <arg>                    Log file where all build output will go.
 -llr,--legacy-local-repository         Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be
                                        activated by using -Dmaven.legacyLocalRepo=true
 -N,--non-recursive                     Do not recurse into sub-projects
 -npr,--no-plugin-registry              Ineffective, only kept for backward compatibility
 -npu,--no-plugin-updates               Ineffective, only kept for backward compatibility
 -nsu,--no-snapshot-updates             Suppress SNAPSHOT updates
 -o,--offline                           Work offline
 -P,--activate-profiles <arg>           Comma-delimited list of profiles to activate
 -pl,--projects <arg>                   Comma-delimited list of specified reactor projects to build instead of all projects. A project can be
                                        specified by [groupId]:artifactId or by its relative path.
 -q,--quiet                             Quiet output - only show errors
 -rf,--resume-from <arg>                Resume reactor from specified project
 -s,--settings <arg>                    Alternate path for the user settings file
 -T,--threads <arg>                     Thread count, for instance 2.0C where C is core multiplied
 -t,--toolchains <arg>                  Alternate path for the user toolchains file
 -U,--update-snapshots                  Forces a check for missing releases and updated snapshots on remote repositories
 -up,--update-plugins                   Ineffective, only kept for backward compatibility
 -V,--show-version                      Display version information WITHOUT stopping build
 -v,--version                           Display version information
 -X,--debug                             Produce execution debug output