get maven to call a method before jar - maven

how do i get maven to run a specific method of mine before it creates the jar file?
basically, i have java code in my junits folder (not a test, just a class with a
main([path to save to])
) that generates an xml file that must be included in the jar. how do i get maven to follow this flow?
compile
run custom method to create xml file passing it the path of the build folder (this method needs the full classpath of the project to run)
jar classes

Bind the maven exec plugin to the generate-resources phase. And your are done.

In short, make it a plugin/MOJO.
In long, philosophy of Maven is not having you to write the build logic inside. It is a more declarative approach.
Anyway, you still have a last resort: use maven-antrun plugin to write a short logic to call whatever u need as ant target.

Related

Invoking maven from inside an uber jar using pom.xml located inside jar

I am running a set of tests using junit and I have created a maven project for it. I use the surefire plugin to run the tests. I wanted to package this project as a self executable jar for the sake of convenience.So I placed all of test classes in the src/main/java directory as recommended for using the maven shade plugin for generating an uber jar. I created a class with a main method and use maven invoker to execute my tests.
The tests get executed perfectly when I use my ide to run the main method. However, after I package it as a jar , the invoker is unable to locate the pom.xml. If I place a copy of the pom.xml in the same directory as the jar, maven is invoked but it is unable to execute the tests as surefire is unable to locate the tests.
I would love to know if there is a solution for this or if there is a better approach towards trying to achieve my goal.
#khmarbaise and everyone else trying to help me , let me describe my scenario a little more in detail.
Lets take any api , for example , lastfm api, I would be writing classes for each api end point and every method in each class would be a test. I use junit to execute the tests. I use a junit wrapper called serenity bdd that helps me structure my tests and generate aggregate reports. The maven goal serenity:aggregate would generate an aggregated report of all tests. I am using the maven invoker to run tests using surefire plugin and then aggregate the tests using serenity. In my eclipse project I simply create a maven run configuration and provide the maven goals. Or else I would use the terminal to run maven from my project base directory.
Apart from my class file, I have several resources, like csv files which are inputs for parametric tests a log folder where all log files that are generated get saved and other sample files for testing file upload apis. I felt that it would be simple to package all of this as a jar and let my developers add this jar as a dependency and run a simple script which runs the jar each time they create and deploy a build. Getting the pom file for invoking maven was trivial in case of eclipse or the command line but not so straight forward when its inside of a package.

How to reference packaged resources from the jasmine maven plugin?

I want to use the jasmine-maven-plugin to test my maven my-webapp project. This project depends on another my-lib project that contains some required JavaScript libraries. When the my-webapp project is built, it adds the my-lib JAR to the WEB-INF/lib/ path of the generated WAR. Inside the my-lib JAR, the needed JS resources are in folders META-INF/resources and META-INF/test-resources.
How can I reference these packaged resources from the jasmine-maven-plugin goals jasmine:bdd and jasmine:test?
Note that I've also tried to run the goals in the integration-test phase like explained here, but I still can't reference the needed resources.
UPDATE: Would running jetty:run-war from within the jasmine-maven-plugin help? If so, how can I achieve that?
I think you would need to first use the maven-dependency-plugin to unpack the jar, under a different goal.
Something like this: unpack dependency and repack classes using maven?
Then you can specify the parameters, under the configuration section of the plugin for that goal, from wherever you unpacked the jar.
wherever/you/unpacked/
Run the unpack goal first, then the bdd and test.

Maven archetype - Run external commands after creation

I'm looking for a way, to execute additional commands (e.g. a perl script) after running mvn archetype:generate on my custom archetype automatically.
Is this possible?
Context
I'm writing an archetype, that creates OSGi bundles which i want to integrate into a parent project as modules. After generating the bundle, i wish to organise it into the parents directory structure and then manipulate poms and other configuration files automatically.
This has been asked on maven forums before 1, however no answer was given.
Thrau, you can invoke mojo post execution of archetype:generate by adding -Dgoals="your custom mojo plugin", within the plugin you can write your custom code. Hope this helps.

Maven, Ant, Dependency Resolution, How to Invoke Target in Another Project

I would like to “extend”, in one maven project, an ant target defined in another Maven Project.
The base project “Project 1” defines an ant build file, with target “BaseDB” that invokes a set of SQL scripts that create a database schema. This project gets built by Maven to a Jar containing the ant build.xml and the dependent SQL scripts.
I would like to define a “Project 2” with an ant build file with a target “ExtensionDB” with depends =”BaseDB,LocalUpdateDB”. That is, ExtensionDB incorporates the BaseDB definitions by reference.
Ive played around with the dependency extensions provided by xmlns:artifact="antlib:org.apache.maven.artifact.ant" without ever being able to resolve anything in Project1/Build.xml (let alone the SQL scripts upon which it is dependent)
Any ideas
I would try to have a lifecycle hook that extract the original Project1/Build.xml from its jar and put it in a place where you can refer it from Project2 build.xml.
(I'm not sure that I fully understand what you're trying to do, but I'd expect the ant extension to resolve the tasks (Java code), not the build.xml file itself.)

How to add files to a maven project executing generate goal (mvn archetype:generate)?

I'm building a maven archetype project. As parameter (serviceDescriptor), I'm passing path to an xml file. When the generate goal is successfully executed, I would like to have the serviceDescriptor file in src/main/resources. Based on maven archetype documentation, it seems that is not possible but, there should be a way to do it.
I have spent couple of days on this and I think that I have found a reasonable solution.
As I mention in the question, I'm passing the file path as required property to the archetype:generate.
I had to implement a simple plug-in that is executed after archetype generate is finishing. This plug-in is coping the file into src/main/resources, read some data from the file and update the pom.xml setting some properties. In order to be able to modify the pom.xml file I'm using maven-model-2.0 archetype as dependency in maven plug-in. It offers Maven MvenXpp3Reader and MavenXpp3Writer classes that allows to safe modify pom.xml.
In order to tell to archetype project to execute plug-in at the end of generate phase of archetype:
mvn archetype:generate -goals=plugin_groupId:plugin_artifactId:goal
The downside is that the plug-in should be available in a accessible repository or local repo.

Resources