maven log file configuration - maven-2

How can I configure logging for maven build? The log which maven generates is not providing enought information like time stamp with each log statement. Where/what log config file maven uses?

You may be aware of this, and it will not print dates, but use mvn -X to print verbose output.
Additionally, you can always pipe the output of maven to some other utility (assuming your shell environment contains halfway competent tools). For instance mvn -X clean | awk '{print "("d")"$0}' "d=$(date)" prints out a date before each line in maven. I didn't bother formatting the date, but that's easily done with arguments to the date executable. Note that this won't really work for maven commands that require interactive user input, such as maven archetype:generate.

Answer provided by #whaley is a good direction. However, the $(date) is evaluated only once at the beginning and then remains the same.
I had to use an approach mentioned in Is there a Unix utility to prepend timestamps to stdin?:
mvn -X <goals> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'

As suggested by #eckes, default logging configuration file is available at /conf/logging/simplelogger.properties, from Maven 3.1.0 onward.
Change "org.slf4j.simpleLogger.showDateTime" property value to "true"
org.slf4j.simpleLogger.showDateTime=true
To change dateTimeFormat, as default is relative time in milliseconds.
Add below line in simplelogger.properties file.
org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS
References:
Maven logging: https://maven.apache.org/maven-logging.html
DateFormats: http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

This still seems some closed issue in Maven, as you can see on:
https://issues.apache.org/jira/browse/MNG-519
The provided workaround looks not too bad, but you need to modify the maven installation.

Maven in Version 3.1 and 3.2 allow simpler configuration of the SLF4J based logger. You can specify "-l logfile" on the command line, and the default configuration of the SimpleLogger is in the config file conf/logging/simplelogger.properties.
If you want to turn on the default timestampts (milliseconds since start) you can simple change the property in this file: org.slf4j.simpleLogger.showDateTime=true.

Related

How to specify output directory via maven?

I would like to change the default output directory (target) to something else. I am using the parallel runner and I expected the command line options/mvn options to overwrite what is specified in the Runner.parallel() call.
What did not work:
Run mvn clean test -Dkarate.options="--output=artifacts"
Configure output directories as specified for the surefire-report-plugin
Configure reportDirectory for the surefire-plugin
What did work (but without maven and without parallel execution):
java -jar karate.jar ... --output=artifacts"
What am I missing here?
Not sure if I fully understand, but pretty sure this is not supported unless you are asking to customize the folder in which the json / xml reports go to - and that is the last argument to Runner.parallel().
Since this is something we've never got as a request, you may want to contribute a PR.

Find classpath maven is using for running testng testcase

what options to maven can I use to determine what classpath maven is running a testng test case with?
You didn't provide the Maven version, but at least in 3.x (and maybe also 2.x) you can run commands with the -X (debug) option. That way the Test Classpath is printed out before tests are run.
mvn test -X
In general you can find the classpath that maven is using by using the built-in maven dependency plugin and its build-classpath goal.
If you want the classpath uses for compiling and running the test you need to select the test dependency scope. This scope is the default, but if you want to be explicit you can set it with -DincludeScope=test.
Other scopes include runtime, compile, provided and system.
Depending on how you want to consume the output you can play with the options -Dmdep.outputFilterFile and -Dmdep.outputFile. The mdep.outputFilterFile makes it easier to parse the output from a script and the outputFile option writes to a file instead which some tools can read directly.
Here are some examples:
$ mvn dependency:build-classpath -DincludeScope=test -Dmdep.outputFilterFile=true|grep 'classpath='
classpath=xxx.jar:yyy.jar
$ mvn dependency:build-classpath -DincludeScope=test -Dmdep.outputFile=cp.txt
$ cat cp.txt
xxx.jar:yyy.jar
I couldn't format my response in my comment so submitting the grepped version here:
mvn test -X | grep "maven.dependency.classpath"

Passing the Maven Debug Flag from Hudson

I'm having an issue with a maven build in hudson. This issue would be fairly easy to resolve if I could see the output of maven with the -X flag passed in. However I can't find a way to do this. If I specify "-X" in the "Goals and options" field of the "Build" section in the job configuration my console output looks exactly the same as if I had not passed the "-X" flag at all. Is the debug logging going somewhere else? Or is there some other way I need to pass the "-X" flag?
UPDATE:
The reason this isn't working is because the build is failing during the "Parsing POMs" part of the hudson process, before it actually kicks off the true maven build and passes in any params I specify in the project. So what I really need is a way to get better logging during the "Parsing POMs" part of a maven build in hudson.
That's what it says in the help text.
Specifies the goals to execute, such
as "clean install" or "deploy". This
field can also accept any other
command line options to Maven, such as
"-e" or "-Dmaven.test.skip=true".
I would have put it there as well.
The maven help gives me the following two options:
-X,--debug Produce execution debug output
-e,--errors Produce execution error messages
May be you can try the --debug or the -e swtich
In Jenkins, and I assume this is true for Hudson as well, command line arguments can be passed to Maven in the "goals" setting. I was able to pass the -X parameter by adding it to the beginning of the "goals".
-X package install
Jenkins apparently blindly appends this setting to the end of the mvn command-line.
I have exactly the same problem with Hudson 1.398 (Maven 2.0.10, jdk 1.6.0_07): the build fails during the parsing of the pom files. It works locally and when launched manually from the Hudson workspace.
That build needs an environment variable to be set (for the path of a module), but the environment variable doesn't seem to be propagated from Hudson to Maven. So, like you, I have also tried to get more details from Maven with the -X option, but it also doesn't seem to be propagated.

How can I supply Maven release prepare information without prompts?

I want to automate the execution of Maven release:prepare with Perl so that the user doesn't have to answer the prompts for version names etc. Are there a -D arguments that can be supplied so that no prompting is done? I tried the obvious solution which is to feed the prompt answers to mvn via perl code like this:
my $cmd = qq(mvn release:prepare -DautoVersionSubmodules=true-DpreparationGoals="clean install");
open MVN, "| $cmd";
print MVN "\n"; # default
print MVN "$cur_version";
print MVN "\n";
print MVN "$next_version";
print MVN "\n";
close MVN;
but mvn ignores such input and winds up using the defaults (and doesn't prompt either).
So, are there -D args for the release:prepare plugin:goal?
Thanks.
You can use the following maven command to do that...
mvn --batch-mode release:prepare
This will assume defaults for anything that you would normally be prompted for; it would be like running a release and simply hitting enter every time it asked you a question. For instance, if your current development version of your project was 1.2.3-SNAPSHOT, it would release version 1.2.3 and move your development version up to 1.2.4-SNAPSHOT. It is usually best to let Maven work this things out for you anyway since the goal of maven is to use convention over configuration. However, is you need to specify non-default properties, the maven-release-plugin allow command line property override as well as using a 'release.properties' file for overriding these settings.
For the Maven part, see Performing a Non-interactive Release.
If it is a Perl solution you seek, command-line arguments are usually executed through the system command.
Try system $cmd;
As far as I can tell, there's no need to open and close filehandles.
You can specify everything using arguments to the mvn release:prepare command:
mvn release:prepare -DautoVersionSubmodules=true -DreleaseVersion={your release version} -DdevelopmentVersion={next version}-SNAPSHOT -Dtag={your tag}

How to display a list of available goals?

I'm rather new to Maven and I often find myself wanting to see what's actually there in terms of goals.
So, is there a command which lists all available goals for e.g. a given prefix?
Since Maven is an open system of plugins, the best answer is probably "Google" ;-). If you mean all build lifecycle phases, they are static, and can be found at http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html and at other places.
Then, for a given plugin, the help plugin can be used to get the possible goals and all their parameters:
mvn help:describe -DgroupId=org.apache.maven.plugins \
-DartifactId=maven-war-plugin \
-Ddetail=true
But this doesn't really answer your question, especially the "for a given prefix" part. For this, the best solution might be to use **auto completion with BASH (**not sure it will be exhaustive though). See for example the Guide to Maven 2.x auto completion using BASH. To get bash completion working under Windows, you'll need CYGWIN. See Maven Tab Auto Completion in Bash for detailed setup steps (and a "better" working auto completion script).
A shorter way
As an alternative, you can also use the -Dplugin parameter to display the list of available goals.
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-war-plugin\
-Ddetail=true
See Maven help plugin.
More and more Maven plugins propose an help goal as alternative to the verbose
mvn help:describe command.
You can read from the Maven doc:
Recent Maven plugins have generally an help goal to have in the
command line the description of the plugin, with their parameters and
types
That is really more natural and pleasant to use.
It works of course for Maven core plugins.
Some examples :
to list goals of the dependency plugin :
mvn dependency:help
to have detail about the javadoc goal of the javadoc plugin :
mvn javadoc:help -Ddetail -Dgoal=javadoc
And it works also for third party plugins.
For example, to list goals of the spring-boot-maven-plugin :
mvn org.springframework.boot:spring-boot:help
[INFO] Spring Boot Maven Plugin 2.0.0.RELEASE Spring Boot Maven
Plugin
This plugin has 6 goals:
spring-boot:build-info
Generate a build-info.properties file based the content of the
current MavenProject.
spring-boot:help
Display help information on spring-boot-maven-plugin. Call mvn
spring-boot:help -Ddetail=true -Dgoal= to display
parameter details.
spring-boot:repackage
Repackages existing JAR and WAR archives so that they can be
executed from the command line using java -jar. With layout=NONE can
also be used simply to package a JAR with nested dependencies (and
no main class, so not executable).
.....
Or to get detailed information about the build goal of the dockerfile-maven-plugin :
mvn com.spotify:dockerfile-maven-plugin:help -Ddetail -Dgoal=build
[INFO] Dockerfile Maven Plugin 1.3.6
Adds support for building Dockerfiles in Maven
dockerfile:build
Available parameters:
- archive
The archive configuration to use for the Docker info JAR. This can be used
to embed additional information in the JAR.
....
You could note that the syntax to get a detailed output of the help and to focus on a specific goal (-Ddetail -Dgoal=myGoal) is exactly the same as this used for the core maven plugins.
Of course some esoteric plugins may not provide the help goal but in most of well designed plugins this is present.