maven - Instruct mvn to find POM upon the path [duplicate] - java

Supposing my maven project is located in /some/location/project and my current location is /another/location/ how can I run maven build without changing to project location cd /some/location/project?

You can use the parameter -f (or --file) and specify the path to your pom file, e.g. mvn -f /path/to/pom.xml
This runs maven "as if" it were in /path/to for the working directory.

I don't think maven supports this. If you're on Unix, and don't want to leave your current directory, you could use a small shell script, a shell function, or just a sub-shell:
user#host ~/project$ (cd ~/some/location; mvn install)
[ ... mvn build ... ]
user#host ~/project$
As a bash function (which you could add to your ~/.bashrc):
function mvn-there() {
DIR="$1"
shift
(cd $DIR; mvn "$#")
}
user#host ~/project$ mvn-there ~/some/location install)
[ ... mvn build ... ]
user#host ~/project$
I realize this doesn't answer the specific question, but may provide you with what you're after. I'm not familiar with the Windows shell, though you should be able to reach a similar solution there as well.
Regards

For me, works this way: mvn -f /path/to/pom.xml [goals]

You can try this:
pushd ../
maven install [...]
popd

If you want to run maven without this command "mvn -f path/to/pom.xml" you can right click on your folder project (in intellij) and click on Rebuild module "name of your artifactId" (corresponding in your pom.xml). It worked for me.

Related

configure maven command line args in settings.xml or set in intellij Idea IDEA

I have multiple projects where I have to pass the following parameters each time.
-Djavax.net.ssl.trustStore=C:\VALUE
-P aws-build
-DskipTests=TRUE
The complete command is:
mvn -Djavax.net.ssl.trustStore=C:\VALUE clean install -P aws-build -DskipTests=TRUE
I am trying to configure it permanently so that I won't have to put it each time I do mvn clean install
I am looking to put settings in settings.xml or anywhere in intellij or any working solution is welcome. The objective is if I run mvn clean install, it should pick up those parameters automatically. Thanks!
Got help from a friend:
In Intellij Idea we can do this:
open pom.xml file -> run maven -> new goal -> add the followings in command line:
-Djavax.net.ssl.trustStore=C:\VALUE clean install -P aws-build -DskipTests=TRUE
->OK.

To include maven in dockerfile or not?

I have this working simple dockerfile.
FROM openjdk:8-jdk-alpine
WORKDIR /data
COPY target/*.jar, myapp.jar
ENTRYPOINT ["java","-jar",myapp.jar]
I build my jar using maven either locally or in a pipeline then use that .jar here. I've seen many examples installing maven in the dockerfile instead of doing the build before. Doesnt that just make the image larger? Is there a benefit of doing that?
Usually I have a CICD server which I use for building my jar file and then I generate a docker image using it. Build a jar consumes resources and doing it when you're running your docker container can take longer depending on your configuration. In a normal CICD strategy, build and deploy are different steps. I also believe your docker image should be as lean as possible.
That's my opinion.
I hope I could help you somehow.
I think you are looking for Multi-stage builds.
Example of multistage Dockerfile:
# syntax=docker/dockerfile:1
FROM golang:1.16
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
Notice the COPY --from=0 ... line, it's copying the result of the build that happens in the first container to the second.
These mutistage builds are good idea for builds that need to install their own tools in specific versions.
Example taken from https://docs.docker.com/develop/develop-images/multistage-build/

How to avoid this error No such file or directory when run maven

I have Backend codes and APIs in a folder I download it from mail. I am using Mac OS.
But when I run ./mvnw in terminal I got the error
-bash: ./mvnw: /bin/sh^M: bad interpreter: No such file or directory
How to overcome this error?
I had the same error as you, installing mvn wrapper in the following way worked for me.
mvn -N io.takari:maven:wrapper
Then cd to the folder where pom.xml file is, and then re-run the ./mvnw command
It looks like your bash gets confused by DOS line endings. This post goes into some detail on how to handle those on MacOS
The error suggests that the mvnw file is not there. Go to your maven project which you need to build and then run mvnw like bellow by pointing to the path of mvnw.
sh /home/user/maven-wrapper-master/mvnw clean install
Or else copy the mvnw file into the maven project directory.
I tried dos2unix filename it works. For using this command we have to install dos2unix
This is the link for install
https://symwisedownload.symantec.com//resources/sites/SYMWISE/content/live/SOLUTIONS/228000/TECH228385/en_US/dos2unix-5.3.3-0.pkg?gda=1539204692_786de4ebe956ae8f24de673f5398dd64
I had the same error, so in this situation ./mvnw doesn`t work, but I tried to write the absolute path to the mvnw file and it works! So try it!

Better way to execute code in a Maven artifact directly? (no project)

I really like the ability to be able to take a Maven artifact that has some useful code in it that can be run directly from the command line. You could run utilities directly from the command line or scripts without first having to download/install anything.
Right now I'm doing it like this inside a script:
mvn -q dependency:copy -Dartifact=${GROUPID}:${ARTIFACTID}:${VERSION}:pom -DoutputDirectory=${TARGET}
mvn -q dependency:copy -Dartifact=${GROUPID}:${ARTIFACTID}:${VERSION} -DoutputDirectory=${TARGET}
mvn -q dependency:copy-dependencies -f ${TARGET}/${ARTIFACTID}-${VERSION}.pom -DoutputDirectory=dependencies
java -cp ${TARGET}/\*:${TARGET}/dependencies/\* ${MAIN}
I tried doing it with the exec plugin, hoping that I could condense that into a single mvn call, but I wasn't able to get it to work.
Is there a better way to do this? (Like in a single mvn call)

Maven - Check a local dependency on Windows

I'm must verify a maven local dependency into a python script. I succeed to do it on linux using this command (from this thread).
mvn dependency:get -Dartifact=g:a:v -o -DrepoUrl=file://~/.m2/repository
My problem is that I'm working with a team using Windows, Linux and Mac, under Windows I'm unable to execute this command, the repo path is the one from mvn configuration maybe the problem is coming from the way I'm calling it as parameter.
mvn dependency:get -Dartifact=g:a:v -o -DrepoUrl=file://C:\Users\user\.m2\repository
Is someone already use this command on Windows and have any hints to solve this problem ?
Thank you .
Finally I reached a solution.
I'm not sure to understand but on windows firstly I must run :
mvn dependency:get
before running :
mvn dependency:get -Dartifact=g:a:v -o
I think the problem about 'dependency plugin' is solved by the first command.

Categories

Resources