Home Navigation

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

Friday, 30 April 2021

Publish maven project to google cloud (GCP) Artifact Registry

  • Enable the Artifact Registry API.
  • Install and initialize the Cloud SDK.
  • Create the repository, you might not see the option maven. It is disabled as it is still in alpha version. To enable the option fill the form. it will take some time to approve. Lets assume your repository name is "quickstart-maven-repo" and location you selected is "us-central1"

  • Now go to your command prompt and login to gcloud console
$gcloud auth login
$gcloud config set project <myProject>
  • Set the repository, run the command:
$gcloud config set artifacts/repository quickstart-maven-repo
  • set the location
$gcloud config set artifacts/location us-central1

  • Create a service account from google cloud console or run the below command

$gcloud artifacts repositories add-iam-policy-binding quickstart-maven-repo --location=us-central1 --member='serviceAccount:ACCOUNT' --role='roles/artifactregistry.writer'

Where ACCOUNT is the ID of your service account in the format USERNAME@PROJECT-ID.iam.gserviceaccount.com

  • Download the service account key

 $gcloud iam service-accounts keys create mykey.json --iam-account=USERNAME@PROJECT-ID.iam.gserviceaccount.com

$export GOOGLE_APPLICATION_CREDENTIALS=mykey.json 
Where mykey.json is gnereated in previous step.

  • Now its time to configure the maven project
  • Choose a Maven project that you want to use. and go to the root directory of the project.
  • Run the following command to print the settings for the default quickstart-maven-repo repository.
$gcloud artifacts print-settings mvn

  • The output should look like below
       <distributionManagement>
  <snapshotRepository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
  </snapshotRepository>
  <repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
  </repository>
</distributionManagement>

<repositories>
  <repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
<releases>
  <enabled>true</enabled>
</releases>
<snapshots>
  <enabled>true</enabled>
</snapshots>
  </repository>
</repositories>

<build>
  <extensions>
<extension>
  <groupId>com.google.cloud.artifactregistry</groupId>
  <artifactId>artifactregistry-maven-wagon</artifactId>
  <version>2.1.1</version>
</extension>
  </extensions>
</build>

Add the output to the pom.xml
  • Run the below command to publish to the repo
$mvn clean deploy
and see the magic. go to google cloud console and then artifact-registry , you should see your published jar
  • For dependent project to access the published jar add the below settings to pom.xml
        <repositories>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
  <extensions>
<extension>
  <groupId>com.google.cloud.artifactregistry</groupId>
  <artifactId>artifactregistry-maven-wagon</artifactId>
  <version>2.1.1</version>
</extension>
  </extensions>
</build>

 and then run the command 
$mvn clean compile

Project should compile and download all the dependency.


Friday, 25 January 2019

Eclipse : Maven search dependencies doesn't work

Eclipse : Maven search dependencies doesn't work


Eclipse artifact searching depends on repository's index file. It seems the index file is not yet downloaded.

Go to Window -> Preferences -> Maven and check "Download repository index updates on start".
Restart Eclipse and then look at the progress view. An index file should be downloading.


wait until downloading is completely done.

Maven Settings

UPDATE You also need to rebuild your Maven repository index in 'maven repository view'.

In this view , open 'Global Repositories', right-click 'central', check 'Full Index Enable', and then, click 'Rebuild Index' in the same menu.

A index file will be downloaded.

Artifact searching will be ready to use.

Tuesday, 8 May 2018

m2e error in MavenArchiver.getMenifest()

If you see the below error in pom.xml

Description Resource Path Location Type
org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration) pom.xml

you may use the below method to resolve this issue.

From Help > Install New Software..

Add the below repository and install m2e archiver

http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-mavenarchiver/0.17.2/N/LATEST/

After installing then update the maven project forcefully

Right click project -> Maven -> update project ( don't forget to check force option)

Tuesday, 2 January 2018

Debugging maven project, breakpoints don't work

By default, Maven runs your tests in a separate ("forked") process. You can use the maven.surefire.debug property to debug your forked tests remotely, like this:

mvn -Dmaven.surefire.debug test

For more information
http://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html


In eclipse got to
Right click on the project -> Run As - > maven build ... -> build configuration

type "-Dmaven.surefire.debug test" in goal section

and add parameter forkCount = 0

Apply and hit debug

Sunday, 3 December 2017

Maven: How to create multiple jars from a single maven project ( maven-jar-plugin)

Two produce multiple jars from a single module project you may use maven-jar-plugin. It is pretty simple to use. In prepare-package phase under configuration you can put your configuration.

below a sample xml file.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-business-jar</id>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifestFile>${basedir}/META-INF/MANIFEST.MF</manifestFile>
</archive>
<includes>
<include>com/**/*</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>com/exclude4/**/*</exclude>
</excludes>
<finalName>jarfilename2</finalName>
</configuration>
</execution>
<execution>
<id>build-webservices-jar</id>
<goals>
<goal>jar</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<includes>
<include>com/walmart/**/*</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>com/exclude1/**/*</exclude>
<exclude>com/exclude2/**/*</exclude>
<exclude>com/exclude3/**/*</exclude>
</excludes>
<finalName>jarfilename2</finalName>
</configuration>
</execution>
</executions>

</plugin>

Wednesday, 29 November 2017

Maven ant run plugin to execute ant command from maven

The example here for scp (secured copy) using ant in maven. One of the tip for this example you declare your dependency inside the plugin tag.



<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>1.8</version>
<executions>
   <execution>
<id>get-ear-from-nexus</id>
<phase>validate|clean|package|</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<scp file="file_name"
todir=" "
verbose="true" password=" " trust="true"
/>
</target>
</configuration>

</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
</plugin>

How to add multiple source file for a maven project

You might sometimes encounter a problem working with multiple source directory for a maven project. we are used to work with single source directory eg: src. what if you have src1, src2. src3 ..

Tor resolve this issue you can use maven helper build plugin.

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>build-helper-maven-plugin</artifactId>
   <version>1.9.1</version>
   <executions>
   <execution>
   <id>add-source</id>
   <phase>generate-sources</phase>
   <goals>
<goal>add-source</goal>
   </goals>
   <configuration>
   <sources>
   <source>
${basedir}/src1
   </source>
   <source>
${basedir}/src2
   </source>
   </sources>
   </configuration>
   </execution>
   </executions>
</plugin>

BDD feature/scenario execution via command line

Run all scenarios
      mvn clean test

Run specific feature file
     mvn clean test -Dcucumber.options="src/test/resources/feature/_file_name.feature"

Run specific scenario within feature file
    mvn clean test -Dcucumber.options="src/test/resources/feature/_file_name.feature:7"
    :7 - line# of the desired scenarios

Run specific scenarios within feature file
     mvn clean test -Dcucumber.options="src/test/resources/feature/_file_name.feature:7:20"

Run scenario by tag
     mvn clean test -Dcucumber.options="--tags @test"

Exclude specific tags from execution
    mvn clean test -Dcucumber.options="--tags @reports, -@tags ~@smoke"


Available environment to execute against – Dev, Test
   mvn clean test –Dcucumber.options=”--tags @test” –P test
   mvn clean test –Dcucumber.options=”--tags @test” –P dev

Thursday, 16 February 2017

Maven's Non-Resolvable Parent POM Problem

Problem: [FATAL] Non-resolvable parent POM: Failure to find com.mycompany.samples:biometrics-samples:pom:4.2.0.0

Solution: Check if the maven installation is correct. Go to eclipse Window->Preference->Maven ->installations

Locate your external Maven installation directory

Then go to Maven->User settings and add the global settings.xml to {MAVEN_INSTALLATION}\conf\settings.xml and update and rebuild index.

Last step, right click on project -> mavne -> update projects and make sure to select force update option as well and click the button OK

It should resolve the issue.

Thursday, 6 October 2016

Publish jar to file system local repository maven deploy

  • Create a local repository directory for example C:\localRepo
  • Here you have two way to publish jar in local repo, either you can create a batch script to publish all of the jars together or open command terminal and execute batch command one by one.
Command Line:
>mvn install:install-file -DgroupId=%groupId% -DartifactId=%artifactid% -Dversion=%versionId% -Dpackaging=jar -Dfile=%JarFile% -DlocalRepositoryPath=C:\localRepo 

BatchScript:
I have added eight jar files to publish, you can adjust the script according to your need.

@echo off
title repository workbrain local setup
color 1f
:: ###################################
cls
:start
echo Maven local workbrain repository setup
call mvn install:install-file -Dfile=axis.jar  -DgroupId=axis -Dversion=axis -DartifactId=axis -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=commons-discovery.jar  -DgroupId=commons-discovery -Dversion=commons-discovery -DartifactId=commons-discovery -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=commons-logging.jar  -DgroupId=commons-logging -Dversion=commons-logging -DartifactId=commons-logging -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=jaxrpc.jar  -DgroupId=jaxrpc -Dversion=jaxrpc -DartifactId=jaxrpc -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=log4j.jar  -DgroupId=log4j -Dversion=log4j -DartifactId=log4j -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=saaj.jar  -DgroupId=saaj -Dversion=saaj -DartifactId=saaj -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=soap.jar  -DgroupId=soap -Dversion=soap -DartifactId=soap -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
call mvn install:install-file -Dfile=wsdl4j.jar  -DgroupId=wsdl4j -Dversion=wsdl4j -DartifactId=wsdl4j -Dpackaging=jar -DlocalRepositoryPath=C:\localRepo
echo.
echo ######################################################
echo All done :-)
echo ######################################################
pause>null



Open pom.xml file of your project and add the below tag under repositories tag


<repository>
      <id>internal local repository</id>
       <url>file://C:\localRepo</url>
</repository>

Then you can add your dependency in the  dependency tag.

Build your project

Publish file system library (jar, war, ear) to remote repository ( nexus) using maven deploy command.

I have created a batch script to publish all the libraries inside a directory, you have to just place the batch script in the directory and execute it.

Before you execute the script make sure that you have defined a server in maven settings.xml (it will be found in {USER_HOME}/.m2 directory)

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <servers>
    <server>
      <id>central</id>
    </server>
    <server>
      <id>snapshots</id>
    </server>
    <server>
  <id>nexus</id> <!-- this is the repository id in batch script -->
  <username>deployment</username>
  <password>deployment123</password>
  </server>
  </servers>

Batch script to publish jars

@echo off
color 1f
::#############################################################################
cls
:start

set repositoryUrl=http://192.168.0.19:8082/nexus/content/repositories/releases/
set repositoryId=nexus
set packaging=jar
set groupId={give_a_group_id}
set versionId={give_a_version}

setlocal EnableDelayedExpansion

echo Reading jar files to publish nexus repository %repositoryUrl%
for %%a in (*.jar) do (
    echo deploying jar file %%~na
    call mvn deploy:deploy-file -DgroupId=%groupId% -DartifactId=%%~na -Dversion=%versionId% -Dpackaging=jar -Dfile=%%~na.jar -DrepositoryId=%repositoryId% -Durl=%repositoryUrl%              
               
)

echo Finished publishing to nexus repository %repositoryUrl%

pause > null

Thursday, 22 September 2016

Common errors in maven projects

 1. -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
  • To get rid of this issue go to window->preference->Installed JRE’s and click the default one and then click on edit
  • Type the below text in Default VM    
    -Dmaven.multiModuleProjectDirectory=%M2_HOME%

Useful maven commands

Create maven project command prompt:

In a terminal /command prompt, navigate to the directory you want to create the project. Type the below command :


mvn archetype:generate -DgroupId={project-packaging}
   -DartifactId={project-name}
   -DarchetypeArtifactId=maven-archetype-quickstart
   -DinteractiveMode=false

Change the version from command prompt:

mvn versions:set
mvn versions:set -DnewVersion=0.8.1