What is the equvalent of npm install in maven - java

i am new to java and have mainly used node.js for most of my projects. I have used npm and used the npm install function to download packages. In maven, is there a similar function that easily installs packages?

mvn dependency:resolve
Is worth take a look to mvn life cycles

There's also mvn dependency:go-offline to download the dependencies and prepare to run the rest of the tasks without any connection to the internet.
I was looking for a way to install maven dependencies during a CI job, while explicitly NOT compiling the code or running the tests.

Keep all the dependencies in your pom.xml(You can think of this as package.json). If you are using an IDE like eclipse it will automatically download the dependencies for you. If you want to download the dependencies from the command line run mvn install.
You can find the corresponding XML for dependencies here https://mvnrepository.com/

Maven doesn't have an install command. Before any task you run maven checks if the project was changed and downloads any new dependency you specified from Maven Central repository.
EDIT: Maven calls the resolver for most tasks you run.

Related

How to find the dependencies of a maven plugin goal

I'm trying to find out where the dependencies are coming from for a particular plugin goal. I know the dependencies exist because Maven downloaded them when I ran the goal for the first time. And they're not (directly) dependencies of my project, because I ran mvn clean install first, and these dependencies weren't downloaded then.
In this specific case, I'm trying to figure out what the dependencies are when I run mvn sonar:sonar, but I expect the answer will be general purpose. For instance, even though I've built this project a number of times, when I ran that goal Maven downloaded a bunch of new jars like maven-antrun-plugin.
Here are things I've tried:
mvn dependency:tree shows the dependencies for the project, but not for the plugin goal (it doesn't include anything related to SonarQube in the list).
mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:effective-pom -Dverbose=true also doesn't include anything related to SonarQube.
mvn -X sonar:sonar prints out what looks like a dependency graph, but it's missing the jars that Maven downloaded the first time I ran the sonar:sonar goal.
mvn -X dependency:resolve-plugins seems to be meant to download the dependencies of plugins, but does not capture the sonar:sonar dependencies. If I clear out my Maven cache, run mvn dependency:resolve-plugins, and then run mvn sonar:sonar, Maven has to download jars.
Use your IDE to navigate to the POM of the plugin project and then look at the dependency tree.
Or do this manually by copying the plugin POM to your file system and running mvn dependency:tree.
The dependencies downloaded by maven are generally stored locally on the pc at the address C:\Users\yourUser/.m2 this regardless of your projects and so that you do not have to reload dependencies that you are already using in other projects.
I hope I have understood your question and that my answer is useful to you, greetings.

in which maven life cycle, dependencies get downloaded from maven central repository to my local .m2 repository

Generally i follow below maven commands to build and run my project.
mvn clean
mvn clean verify
or
mvn clean install
mvn spring-boot:run
My doubt is in which maven life cycle, dependencies get downloaded from maven central repository to my local .m2 repository.
I went through below mentioned maven life cycle but no where i found that in this steps dependency gets downloaded.
validate
compile
test
package
verify
install
deploy
Please explain it would be really helpful.
When you create a maven project, before validate there is step 'prepare-resources' which copies resources. Also when you do maven clean it will download dependencies. Read this link for more details
https://www.tutorialspoint.com/maven/maven_build_life_cycle.htm
The other answer here of prepare-resources is incorrect. That may be confusing the downloading of Maven plugins and their dependencies, but not the project's dependencies.
They actually are downloaded during the compile lifecycle.
Here is an example of a project where the only dependency is GSON, and I just finished running the process-resources lifecycle, the one that immediately precedes compile lifecycle. The only things present in my .m2/repository directory are things required by the default Maven plugins. Note that there is no com folder, which is where GSON would have been downloaded to.
After running mvn compile, the next lifecycle, a lot more dependencies are downloaded, including GSON:

Want to build project with commandline mvn but have to use Maven update in eclipse first

I'm working with multiple projects in Eclipse. because I want to automate the building I want to script the building process.
Unfortunately I cannot do the same actions on the commandline as in Eclipse.
So a common problem is that when a new function from a referenced project is used, I cannot build the project on the commandline with mvn. I use the command:
mvn clean install -U
But this command will give a build failure until I do a Eclipse Maven Update from the eclipse GUI. After that I can build the project again.
I also tried all the other commands I came across Stackoverflow:
mvn eclipse:eclipse
mvn dependency:resolve
So I just want to that Maven Update command in eclipse from the commandline so I can build from the commandline. If anyone could tell me what I'm doing wrong that would be awesome.
Thx in advance
Update for more clarification:
The project structure is:
Rest-service, Framework-service, Framework-model
Framework-model is referenced in the pom file by Framework-service and Framework-service is referenced by Rest-service. The other projects are not relevant to the problem.
When a function is added to Framework-model and used in Rest-service it gives an compilation error in eclipse and when I build with mvn clean install -U, although Maven install in eclipse is succesful but I think it is still using the old compiled code. After a Maven Update command in eclipse the compilation error is gone. And mvn clean install -U from the commandline also works.
How could I do a Maven Update command in the commandline? If mvn clean install -U should also do a Maven Update command, what settings should I check?
Another update: So this weekend I tried different things and running mvn compile before the mvn clean install -U command gives a different output. And finds the new function. But as I read maven, I thought install should also do the previous steps. How is this possible?
Eclipse's Maven plugin uses the maven version configured in Preferences/Maven/User settings. If you have a different version of maven in your Eclipse's settings than the one on your PATH variable, you could have different outputs. Maybe try and check that.

How do I build this java project with maven?

I honestly don't know anything about java, I'm just trying to build the latest version of this project to try and work around bugs in the version that is packaged by my OS. The project in question:
https://sourceforge.net/projects/tuxguitar/
I tried running mvn compile, mvn clean package, and mvn clean install but no jar file was produced in the source tree. The readme has no instructions for building. Any help would be greatly appreciated!
Thanks!
To produce jar you need at least package phase, you can get there by doing:
mvn clean package
(clean is to remove stale data, it is a good practice to use it always with maven) and you jar will be in target directory.
mvn complite just complite the project and no package.
answer : mvn clean package.this will clean,complite and package the project,after finished, you can found the package(jar or war) into the $PROJECT_HOME/target/ folder.
I usually run mvn clean install - which in addition will add the built jar into your local maven artifact repository.

Project specific local repository in maven

Is there a maven equivalent of Node.js npm i which fetchs all the dependencies under node_modules directory, or Ruby bundle install --path <directory-path>?
I'm looking for a way to manage the dependencies written in pom.xml on the project's own responsibility. Without anything, all the artifacts are downloaded into ~/.m2/repository which is shared by all existing maven projects.
The version of maven is 3.1.1. Is there any good idea?
If you really like to change the repository for every project you can use the following:
mvn -Dmaven.repo.local=/WhatEverDirYouLike clean install
but it contradicts to the idea of the local repository.

Categories

Resources