Send maven output to file - maven

On Windows 7, I am trying to send the output of a maven-3 command to a text file.
I call the command from the root of the project I am trying to analyze.
The command is:
mvn dependency:tree -Dverbose -Dincludes=commons-collections -DoutputFile=C:\Users\myname\Documents\output.txt
When I run the command without the outputFile parameter, I see the output sent to the console.
But when I use it with the outputFile parameter, the output file is empty.
Any idea what I am missing here?

Try mvn -help
-l,--log-file <arg> Log file to where all build output will go.
mvn <your parameters> --log-file log.txt

Just give it a try:
mvn dependency:tree -Dverbose -DoutputFile=resout.out
within the same folder where the pom file is located.

Old school, but it's what I knew. One caveat is that the mvn command does not return when done to the cli, but for some purposes this is acceptable.
mvn "-Dexec.args=-classpath %classpath com.mycompany.test" -Dexec.executable=/Downloads/jdk1.7/bin/java exec-maven-plugin:1.2.1:exec > /tmp/Out

Related

Gitlab Runner: return code of "mvn verify" changes when redirecting output

Note: I am using a windows build node with powershell.
Previously, I was executing the following in a gitlab CI job:
- mvn --version
- mvn verify -fae
- mvn -D"tycho.mode"=maven jacoco:report -D"jacoco.dataFile"="../target/jacoco.exec" sonar:sonar
Now, I am redirecting the output of mvn verify -fae to a file:
- mvn verify -fae > testOutput.txt 2>&1
The build output is written to the desired file, but apparently, the exit code is "1", even though the tests complete successfully.
If I do not redirect stderr to file, i.e. leave away 2>$1, everything works okay. Another strange thing: if I wrap the command in a powershell script, the $ERRORLEVEL variable is 0, so the exit code should be 0.
Does anybody know what could be going on?
At first you have to make shell to redirect File Decriptors (FD) and only then you should define a file.
- mvn verify -fae > 2>&1 > testOutput.txt
If you need to redirect your output to file and also see it in your session so you may use 'tee':
- mvn verify -fae 2>&1 | tee testOutput.txt

Batch file stops executing after mvn command

set homepath=%userprofile%
set a=%1
set b=%2
set c=%3
set uuid=%4
set zipDirectory=%5
set pluginDirectory=%6
cd %homepath%\%a%
mvn archetype:generate -DarchetypeCatalog=file://%homepath%/.m2/repository
Everything works up to here, then the command lines stop executing. It doesn't print the 1, nor subsequent commands.
1
c
b
c
%uuid%
Y
cd %homepath%\%a%\%b%
mvn clean install
cd %homepath%\%a%\%b%\%b%-plugin\target
jar -xvf %zipDirectory%
cd %homepath%\%a%\%b%\%b%-plugin\target\META-INF\maven\%c%\%b%-plugin
copy pom.xml + %pluginDirectory%
cd %pluginDirectory%
rename pom.xml %b%-plugin-1.0.0.pom
Question: Is there anything about maven I don't know about that interrupts a batch process? Does it not understand to execute the command with a lone number 1?
When invoking Maven from a batch file to create a new project via archetype you should be aware of the interactive mode of the execution, that is, Maven will prompt for certain values or ask for confirmation.
It seems in your case this is not the desired behavior. You should hence pass via command line some options of the generate goal and the specific archetype and then run either in batch mode via the -B standard Maven option or via -DinteractiveMode=true.
From official documentation you should pass
The archetypeGroupId, archetypeArtifactId and archetypeVersion defines the archetype to use for project generation.
The groupId, artifactId, version and package are the main properties to be set. Each archetype require these properties. Some archetypes define other properties; refer to the appropriate archetype's documentation if needed
Hence in your case:
call mvn archetype:generate -DarchetypeCatalog=file://%homepath%/.m2/repository -B \
-DarchetypeGroupId=com.sample -DarchetypeArtifactId=artifact -DarchetypeVersion=1.0 \
-DgroupId=your.groupid -DartifactId=your.artifactId -Dversion=0.0.1-SNAPSHOT \
-Dsomething-else=value
Note: \ added for readability, you don't actually need it
try to execute that command like this. Then next command will execute as expected
CALL mvn <command>

Can't execute "mvn clean package" task in GO CD

I have setup a "Hello World" pipeline with one task mvn clean package in Go CD. I have registered an agent with Java and Maven up and running.
When I trigger the pipeline, the job fails:
12:05:08.655 [go] Start to execute task: <exec command="mvn" > <arg>clean package</arg> </exec>.
12:05:08.660 Error happened while attempting to execute 'mvn clean package'. Please make sure [mvn] can be executed on this agent.
If I execute mvn clean package in my agent, everything works. What is happening? Is there a place where I can see more specific logs?
Instead of running the following:
Command:
mvn clean package
try to use
Command:
/bin/bash
Arguments:
-c
mvn clean package
I struggled with the same issue and figured out a solution which worked for me. Maybe, it will help you.
In the command prompt of your agent enter echo $PATH
This will show all your path variables. Copy all of them.
Now, in the GUI of your server, choose the configuration of your
pipeline and add a PATH variable with the copied variables.
You can use:
Command:
mvn
Arguments:
clean
package

Executing maven compile commands from bash sequentially

I'm trying to build a project with Maven sequentially, with no success.
I tried put the following lines in a script file and called it from bash.
mvn -f ./cmroad-api/pom.xml clean install corona:package -Ddebug=false
mvn -f ./cmroad-impl/pom.xml clean install corona:package -Ddebug=false
The first command runs but the build does not call the second command. I tried putting the ; like:
mvn -f ./cmroad-api/pom.xml clean install corona:package -Ddebug=false; mvn -f ./cmroad-impl/pom.xml clean install corona:package -Ddebug=false.
The output was:
[ERROR] Unknown lifecycle phase "mvn". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>.
I would be most thankful for any help.
[ERROR] Unknown lifecycle phase "mvn".
This is the error you would get on typing mvn mvn; your script has two mvns on the same line, presumably because something is wrong with your line endings.
Start with a simple script:
#!/bin/sh
echo A
echo B
and get that working before moving on to Maven. The commands you specify should work when placed in a correctly prepared script. (Although, as khmarbaise says, your project is wrong if you need to build in this order.)

Stop logging downloads from repository on Maven

Is there a way to remove redundant output lines that informs about every small download Maven made from the repository. I only want to see output of the actual plugins.
Is there a plugin that is in charge of all this output that I can configure?
Thanks!
I dont think you can achieve by changing maven settings.
Only options i knw are mvn -q hides the [INFO] lines and mvn -X shows the debug messages.
You should look to save the log messages in a file ans then use unix grep command to filter messages which you want.
To suppress downloading/downloaded messages in maven I use the following command:
mvn --batch-mode ... bla bla bla ... | grep -v 'Download.* http'
It is will suppress downloading/downloaded messages without suppressing anything else, i.e. the uploading/uploaded [INFO] messages, which anyone in their right mind would want to keep.

Resources