17

I've just installed Maven 3 - I'm new to Java and I want to learn how Maven works. For now, I know that I can add a dependency by editing POM file in the maven project, but I wonder if there is a 'clean' way to do it with command line. Running Windows 8.1, Java 8, Maven 3.

Lurker
  • 267
  • 2
  • 4
  • 10
  • Also, is there any way to use some kind of external repository like mvnrepository.com? – Lurker Mar 21 '17 at 15:46
  • Note that adding a jar to your local m2 repo does not mean that the pom will use it – bichito Mar 21 '17 at 16:55
  • What does "m2" mean? Maven 2? – Lurker Mar 22 '17 at 16:53
  • yes. In your home folder you will find it. In *nix OS it's hidden (.m2) in Windows, I don't know. – bichito Mar 22 '17 at 17:37
  • You can't, that would be too simple. You need to manually find the latest version, edit an XML file, find the right place to put it and wrap everything in 3 different XML tags and close those tags because everything in Java has to take extra time so you can charge more money for all those hours your Java programmers work. – Boris Verkhovskiy Jun 10 '23 at 15:49

2 Answers2

6

I'm not very sure if there's a built-in maven feature or plugin to do so, but I don't think so. You could achieve it by a small script using sed or something similar. I've used the following in a bash script:

get_foo_dep() {
  cat <<EOF
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>foo</artifactId>
        <version>${DEP_VERSION}</version>
    </dependency>
EOF
}

DEP_VERSION="1.0.0"  # just example, you can set it from somewhere
POM_FILE="pom.xml"   # just example, the path to pom

foo_dep=$(get_foo_dep)
foo_dep=${foo_dep//$'\n'/\\$'\n'}   # escape \n with \\n for sed to work
sed -i "s|<dependencies>|<dependencies>\n${foo_dep}|" "$POM_FILE"  # CARE!! it makes in-place substitution

You can also have a look at this other question, where awk is recommended:

Is it possible to use sed to replace dependencies's version in a pom file?

Gerard Bosch
  • 648
  • 1
  • 7
  • 18
3

Using the POM directly is probably a better way, but yes you can do that:

mvn install:install-file -Dfile=<path-to-file> -DgroupId="group-id" \
-DartifactId="artifact-id" -Dversion="version" -Dpackaging="packaging"

https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • Seems to work fine with downloaded *.jar files. Is there a way to use external repositories? (see comment to the thread) – Lurker Mar 21 '17 at 15:49
  • https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html - this might be of use – achAmháin Mar 21 '17 at 15:51
  • 2
    This adds a jar to your local .m2 repo. But if the pow does not include it, it will ignore it – bichito Mar 21 '17 at 16:53
  • 9
    The OP asks for a way to add a dependency in a project via CLI instead of manually editing POM; while your answer is about installing a JAR in the local Maven repository. Two different things. – Gerard Bosch May 22 '20 at 16:05
  • 1
    I dont know why this is the best answer. The question is about editing pom using command line... – danipenaperez Mar 12 '21 at 13:13