Add Maven repo into class path - java

I have a Maven repo which contains all of the dependencies needed for a Java project.
How do I add the maven repo into the classpath environment variable so it can be access via the console?
Working with a project that has a bash script to setup the development environment. This also runs some Java code but is can not see any for the dependencies of the project.

You can configure either the setting.xml at user level (${user.home}/.m2/settings.xml) or the global level configuration (${maven.home}/conf/settings.xml) by specifying the localRepository element to define the local repository path. Setting the environment variable MY_M2_REPO, before running maven, will effectively define the local repository path.
<localRepository>${env.MY_M2_REPO}</localRepository>
Alternatively, you can use the argument maven.repo.local to specify the location of your local repository when running maven.
mvn clean package -Dmaven.repo.local=/home/user/maven_repo

Related

Not able to import maven dependencies in eclipse ? Tried everything

I am using apache maven 3.6.3(not the embedded one in eclipse).
I have tried the same with the embedded maven also , still facing the same error.
Also i have deleted the whole m2 repository and again re-import the whole project but still the maven dependencies are not installing.
Maven dependencies
Assuming windows and as other answer you have a pom.xml:
Delete your repository at C:/Users/????/.m2/repository
Check your C:/Users/????/.m2/settings.xml for correct proxy config
Set you M2_HOME environment variable to <MAVEN INSTALL DIR
Add %M2_HOME%/bin: to your PATH variable assuming windows (remember to start a new window after changing environment variables)
Try a mvn clean install from the command line - GET THIS WORKING FIRST
Then try from eclipse, you do not need a proxy setting it should call the default mvn.
If you convert your project to maven project pom.xml will be automatically created,if not
1- Create pom.xml file -by clicking on project directory->new->file
2- search from google maven dependencies for ex: if you want to download spring-context.jar ,then search for maven spring context ,you will get groupid and artifactid paste it in pom.xml file.jars will be automatically downloaded.

maven: how to specify "systemPath" for dependencied installed locally?

I've got one myPackage maven project, compiled and installed to local maven repository under
~/.m2/repository/mygroup/myPackage/1.0-SNAPSHOT/myPackage-1.0-SNAPSHOT.jar
In another maven project, I wish to use it, and in pom.xml I write <dependency> section for it. But I don't know how to write the "systemPath" for this jar:
I cannot use "~" to specify the path, because "~" is a *nix shell extention, java/maven cannot recognize it.
I cannot hard code like
/home/myself/.m2/...
It's not portable.
I cannot use ${project.basedir} because these 2 maven projects are under different folders. But I guess there should be some other maven environment variables that could indicate "home directory"?
All I wish to do is to get this "systemPath" done.
---------------Problem solved by using another project as dependency------------
<systemPath>${project.basedir}/../myPackage/pom.xml</systemPath>
That works!
A system path is required when the library that your project depends on is not in the maven local repository.
As a rule of thumb, this approach is indeed not portable at all and should be avoided for real projects.
Now, the dependency is in local repository if:
It was downloaded from some remote repository (usually)
You've installed it locally (in this case its in your local repository but might not be in your team-mate repo)
In order to install the dependency into the local repo consider using: mvn install:install-file+ parameters as written here
But from your question, it looks like the file is already there... Anyway once the file is in the local repository you can just define a "regular" dependency (group, artifact, version) and Maven will pick it, no need to fiddle with system Path in this case.

Run maven with specific path to certain plugin from command line

I would need to execute a specific Maven plugin from command line. For example, in the following I execute a specific version of Maven Surefire Plugin to test Java projects:
mvn org.apache.maven.plugins:maven-surefire-plugin:2.19-SNAPSHOT:test
However, the above assumes to find the surefire plugin 2.19 in the default Maven repository path. Now, my question is, if I want to use the plugin with a specific path (not Maven default one), what should I do using the command line? I would expect something like the following, without modifying pom.xml:
mvn /path/to/some/jar/version/org.apache.maven.plugins:maven-surefire-plugin:2.19-SNAPSHOT:test
or more generally, for the following invocation
mvn groupId:artifactId:version:goal
I would need somewhere to specify a customized path to execute its goal
mvn /some/path/to/groupId:artifactId:version:goal
On the other hand, please let me know if this is not even supported by Maven.
This is not how it works. Maven will always look-up artifacts inside your local repository. And if it can't find it in your local repository, it will try to download it from configured remote repositories.
As such, you don't specify a path to a plugin. You specify a path to a local repository, where that plugin is installed. In this local repository will be installed all the dependencies of the plugin you're trying to invoke. This also means that you cannot have a JAR to a plugin "sitting around" anywhere; it needs to be located inside a correct repository tree directory. With a local repository of /my/local/repo, the artifacts of the plugin groupId:artifactId:version must be located in /my/local/repo/groupId/artifactId/version and be named artifactId-version.jar (and .pom). In the same say, the location of the dependencies of that plugin must follow that directory structure.
By default, the local repository is located inside ~/.m2/repository. But you can change that by:
Specifying the maven.repo.local system property on the command line, for example with mvn -Dmaven.repo.local=/path/to/local/repo groupId:artifactId:version:goal;
Use a custom settings.xml and tell Maven to use it with the -s command line option. It would contain:
<settings>
<localRepository>/path/to/local/repo</localRepository>
</settings>
and be used with mvn -s /path/to/settings.xml groupId:artifactId:version:goal

Add list of directories to Maven

I have an environment variable called $MYCLASSPATH that contains a set of directories that contain JARS. An example of my environment variable could be the following:
/project1/jars/:/project2/jars:/project3/jars
I also have a maven project that contains some external dependencies that are defined in the pom.xml. However, I want to include all the directories listed in the above environment variable in Maven since some JARS are required for the compilation.
Without Maven I could do this:
javac -cp "$MYCLASSPATH" path/to/my/java_file
How can I add all these directories to Maven?
As an addition to crowne's answer, once you know the coordinates the external dependencies would have to be added to your local maven repository using mvn install:install-file
For example
mvn install:install-file -Dfile=path_to_your_jar -DgroupId=X -DartifactId=X -Dversion=X -Dpackaging=jar
Your POM can then contain these dependencies in the same way normal dependencies are declared eg.
<dependency>
<groupId>X</groupId>
<artifactId>X</artifactId>
<version>X</version>
</dependency>
Each jar should be identified by a specific set of maven coordinates
https://maven.apache.org/pom.html#Maven_Coordinates
This lets us know which version of the jar the project is dependent on and where to get it from in the maven repository.
It also allows the project to be built correctly from another computer with a different set of environment variables.
So you should define your dependencies in your pom using the correct maven coordinates.

How should I configure the eclipse to make use of different values of environment variable for different maven phases, during maven building process?

Prehistory
Our project builds with maven. We use eclipse.
In project i have two DB property file:
jdbc.test.properties
jdbc.prod.properties
with the same structure:
jdbc.driverClassName=
jdbc.url=
jdbc.username=
jdbc.password=
value of username and password in files are different. So we use different schemes for testes and production.
In data-access-config.xml i have the following line:
<context:property-placeholder location="classpath:jdbc.${db.flag}.properties"/>
What i do:
i click "maven install" (in context menu in eclipse)
maven building process runs
What i want:
the environment variable "db.flag" was set to "test" during test
phase
the environment variable "db.flag" was set to "prod" during other
phases
Now i use bat file:
call mvn test -Ddb.flag=test
call mvn install -Ddb.flag=prod -Dmaven.test.skip=true
But it is not an issue for my team.
How should I configure the eclipse to make use of different values of environment variable for different maven phases, during maven building process?
I'll be glad to any advice.
I suggest keeping each copy under the same name (jdbc.properties) in src/main/resources and src/test/resources likewise, so the test phase can pick up your 'test' settings automatically and your location attribute is no longer needed.
Maven will make sure that the test resources are only used at the test phase.

Categories