Is it possible to execute a Maven plugin from the command line? I need to run dependency plugin:
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${RPTBIN}/.tools/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
</executions>
Is there any way to execute this plugin just like this plugin executes during Maven build?
You should be able to run it with just mvn dependency:copy-dependencies and just add the relevant configuration parameters with -Dparameter=value, i.e. -DoverWriteReleases=false
Yes that is possible. Maven is a Java tool, so you must have Java installed in order to proceed. Please go through the installation process here.
mvn dependency:copy-dependencies
A sample dependency plugin command. By the way how you have been running the Maven command till now?
You can use below command. It worked for me:
mvn {your groupId}:{your artifactId}:{your version}:{your goal}
But remember this command is good if your plugin main class is extending AbstractMojo.
It will run the execute() method of your plugin main class.
Also, before running this command please run
mvn clean install to build the jar
Related
I want to change maven project's active profile from within the maven project.
The java project has 3 classes having main methods and they are executed one by one, All of these require different set of dependencies.
I would like to run these classes with different maven profiles.
For example my project project1 is a maven project and is getting invoked by mvn clean package spring-boot:run command.
When the control comes to the project1 it executes the java classes one by one.
My requirement is that when ClassA is run it should change the current maven profile to profile1 (recompiling the same project is ok, performance is not priority). Similarly when ClassB gets invoked it should change the current maven profile to Profile2.
(Note these classes are dynamically picked from directory using reflection and only the class will know what profile it supports)
Is there any way maven allows this at runtime.
If not what is the best way to achieve this.
maven profiles are not designed for such tasks, what you are actually need to do is to setup different executions for each class:
...
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>run-clsa</id>
<goals>
<goal>run</goal>
</goals>
<phase>none</phase>
<configuration>
<mainClass>ClassA</mainClass>
</configuration>
</execution>
<execution>
<id>run-clsb</id>
<goals>
<goal>run</goal>
</goals>
<phase>none</phase>
<configuration>
<mainClass>ClassB</mainClass>
</configuration>
</execution>
</executions>
...
and run that in following way:
mvn clean package spring-boot:run#run-clsa
mvn clean package spring-boot:run#run-clsb
I am using exec-maven-plugin to execute a shell script.
<executions>
<execution>
<id>exec-ui-install</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>${basedir}/ui-build.sh</argument>
</arguments>
<skip>${exec.skip}</skip>
</configuration>
</execution>
</executions>
I dont want this to run during build time (as i don't need to run it during build and i am building it from windows). So i am using a parameter named exec.skip and with its help i am able to skip it.
After building jar and moving it to Linux env i am using java command
Ex: java -cp : javaclass
to run the jar. During this i need to execute "exec-maven-plugin" which was disabled during build mode. How do i pass "exec.skip=true" through java command so that i can run plugin.
You cannot do it.
The maven configuration of a project is used during the build of the project.
Once the artifact/component is constructed, you don't interact any longer with maven.
In your case, you should build your component with the suitable configuration parameters before moving it to linux.
Using a Maven profile with this specific configuration that is launched by a continuous integration tool could ease the task and make it reliable.
I need to run ActiveMQ in my maven clean install, so that it starts up activemq and then runs my tests. I have included the plugin in pom.xml and added the mq config file for configuring mq details. It runs successfully and all test pass when I run activeMq on one console and run maven clean install on others (2 separate commands on 2 separate consoles). But is there a way I can run activemq and clean install both with 1 command on the same console? Basically I wish that when I do mvn clean test install, it should automatically fire-up mq first and then proceed with the tests...
I have tried using commands like mvn activemq:run clean test install or mvn clean test install activemq:run. But it either does the clean install or runs the activemq...
How do I combine these 2 commands(activemq:run and mvn clean test install)?
Attach the maven goal and plugin using the <build><plugins> construct.:
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq.tooling</groupId>
<artifactId>maven-activemq-plugin</artifactId>
<version>...</version>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<phase>process-test-classes</phase> <!-- or other phase before the test phase -->
</execution>
</executions>
</plugin>
</plugins>
</build>
I have a multi-module Maven project with a master pom and numerous sub-directories each containing projects (and pom.xml files) which refer to the master pom.
In projectA contains an invocation of exec-maven-plugin, which executes the java goal and successfully invokes a test class that resides somewhere in projectA (in projectA/test/com/mycompany/Testclass.java). The plugin is declared as follows:
org.codehaus.mojo
exec-maven-plugin
1.2.1
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
I execute maven with the following command, and it works fine:
mvn verify -P <profile name> -pl projectA -am
I have a second project, projectB, which depends on projectA. I have configured an exec-maven-plugin section in projectB's pom.xml file that is identical to the one above. When I run maven with the same command as above (except with projectB in the -pl parameter), I get the exception java.lang.ClassNotFoundException: com.mycompany.Testclass.
Clearly this is happening because the classpath when I run maven for projectB does not include the directories in projectA, even though the pom.xml has a dependency on projectA (actually it depends on projectA.1, which in turn depends on projectA).
I have tried switching to use the exec goal rather than the java goal, in projectB, and then providing a classpath in the arguments, like this:
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<argument>"../projectA/target/projectA-6.6.1-bSOURCE-tests.jar;../projectA/target/projectA-6.6.1-bSOURCE.jar"</argument>
<argument>com.mycompany.Testclass</argument>
</arguments>
<classpathScope>test</classpathScope>
</configuration>
</execution>
When I've done that I successfully load the class, but get a ClassNotFoundException on com.google.gson.JsonSyntaxException
That happens to be the same error I get if I run the class from the command line, as follows
java -classpath "projectA/target/projectA-6.6.1-bSOURCE-tests.jar;projectA/target/projectA-6.6.1-bSOURCE.jar" com.mycompany.Testclass
In projectB I think I want to use the java goal of the exec-maven-plugin rather than the exec goal, but one way or the other I have to be able to specify the classpath.
Any ideas how I can do that?
Thanks.
I figured this out myself, and am posting my answer in case anybody else needs it, or in case anybody wants to dispute it (or suggest something better).
What I did was add a dependency in the subordinate project, projectB, as follows:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>projectA</artifactId>
<version>6.6-SNAPSHOT</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
I believe the clincher was the element, which directed maven to get the dependent classes from the jar file containing the test classes in projectA.
I've got some projects that are already doing site generation via maven, and I want to integrate cobertura reports in them, but no maven goal I seem to run will generate a local preview for me to look at that includes the Cobertura reports in the site. I want to be sure they're generating correctly before I commit the pom changes to the repo and have broken site generated.
Below is what I've added to the maven poms (parent and module), but the site I see when I run mvn site:run does not include the cobertura reports:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<check>
<haltOnFailure>false</haltOnFailure>
<regexes>
<regex>
<pattern>parent-package-name-here.*</pattern>
<branchRate>80</branchRate>
<lineRate>80</lineRate>
</regex>
</regexes>
</check>
<instrumentation>
<includes>
<include>parent-package-name-here/**/*.class</include>
</includes>
</instrumentation>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>instrument</goal>
<goal>cobertura</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
...
</project>
What maven command should I use to generate the site with cobertura reports? Or, what should I add (additionally) to get the site generation to include the cobertura reports?
Should do:
mvn site
To elaborate, running mvn a:b runs the goal b in plugin a. Saying mvn c means to run the lifecycle phase c, which runs all of the bound goals in all of the phases up to c. As a result, this will trigger a lot more things to happen (such as doing the necessary preparation to produce cobertura reports).
I figured out how to do this.
It seems there are a lot of bugs in the link generation within the maven site generation plugin.
The only way I've found to make maven generate a local copy of the site with working module links is to modify the distributionManagement/site tag to point to some local directory instead of the real-live deploy directory, then use maven site:deploy.
Every attempt to use mvn site:stage generates broken links. Same goes for mvn site:run.
The report links work with mvn site:run / mvn site:stage but the links to modules do not.
mvn site
should do what you are looking for. You configure the plugin to run in the pre-site and site phases of the life cycle but your are then executing the site:run goal not site. We are doing similar things with clover (commercial coverage tool) and mvn site does the trick.
site:stage module links don't work in my experience either for multi module builds but site:deploy does. Try this:
Use a property for the site URL in the parent pom, e.g. ${site.url}. Then call this
mvn clean site site:deploy -Dsite.url=file://`pwd`/target/site-deployed
The pwd is a -nix command that will substitute the current directory. This is because the URL that you use must be absolute.
We use
mvn site-deploy
This builds the site and deploys it (copies it to the place we have configured).
mvn site:site should produce what you are after, in the target directory, there will be a site directory containing all reports linked with an index.html in that directory.