Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making maven do file copying between building and running

Tags:

maven

Revised Question (old question below):

Problem: When building and running a Jenkins plugin project in NetBeans using Maven, Java class loader used by log4j does not find some files (like configuration file, a custom appender, JDBC driver). They need to be copied to another location under target, and then they are found.

The solution I think I want is this: I want to give maven an extra command line argument(s), configured in NetBeans. This should make maven do extra step after building and before running anything. The step can either be the file copying (specified in pom.xml or by the command arguments), or it can be running a script (specified in pom.xml or by the command line arguments).

What to add to pom.xml? Some maven plugin?


Old Question (for reference)

I have a maven project for "my-jenkins-plugin". I need to use log4j with it. I currently have log4j.xml at this path:

./src/main/resources/log4j.xml

When I do clean&build in Netbeans, it gets copied to these places:

./target/classes/log4j.xml
./target/generated-classes/emma/classes/log4j.xml
./target/my-jenkins-plugin/WEB-INF/classes/log4j.xml

However, log4j does not find the file from any of these locations. I need to manually copy it to this location:

./target/work/webapp/WEB-INF/classes/log4j.xml

And then it works as expected.

More details: Maven 2, Netbeans 7.2. It's standard Jenkins plugin, project originally created with mvn hpi:create, and Netbeans uses Jetty to run Jenkins. Classpath used when running under NetBeans is java.class.path=C:\work\maven\boot\classworlds-1.1.jar (which is an existing jar) and I haven't found a way to add anything to this classpath, but if I add log4j.xml to root of that jar file, it is also found and works (obviously this is not a satisfactory solution). I know if log4j works or not simply by looking at console output, either it's correct log lines, or the dreaded log4j:WARN No appenders could be found for logger (TestLog). log4j:WARN Please initialize the log4j system properly. error and nothing else.

Question: How can I have either

  • the log4j.xml found in the first set of locations,
  • or log4j.xml found in a location outside target dir, like C:\log4j\log4j.xml
  • or make maven and/or netbeans to copy log4j.xml to a location where it is found now
  • or any other nice way to let me give my plugin log4j.xml after clean&build automatically, when debugging with NB?
like image 314
hyde Avatar asked Dec 10 '12 10:12

hyde


People also ask

How do I copy files from one directory to another in Maven?

Using the Copy Rename Maven Plugin The copy-rename-maven-plugin helps copying files or renaming files/directories during the Maven build lifecycle. Upon building the project, we'll see foo. txt in the target/destination-folder.

What does the mvn clean command do?

1. mvn clean. This command cleans the maven project by deleting the target directory.

What is Maven copy dependencies?

Full name: org.apache.maven.plugins:maven-dependency-plugin:3.3.0:copy-dependencies. Description: Goal that copies the project dependencies from the repository to a defined location.


1 Answers

You could use the Maven Ant plugin to run an Ant script that did the copying, or use the Maven resources plugin, which seems aimed at your exact use case of copying stuff to the output directory.

Also be aware that you can parameterize your build as described here (ignore the Jenkins part of that title, the answer relates to general Maven usage).

like image 150
Andrew Swan Avatar answered Sep 23 '22 11:09

Andrew Swan