I have an executable jar file. I can start it properly using command java ${JAVA_OPTS} -jar.
I deployed it to internal maven repository and include it to spring boot app as an dependency. How can I execute that dependency as jar file? I want to create Schedule to execute it every day.
One of the possible option is unpack jar via maven plugin, update main class in spring-boot-maven-plugin and execute the command as mentioned below :
Unpack the jar :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.group</groupId>
<artifactId>artifact</artifactId>
<version>0.0.1</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Update Main class(add below tag in spring-boot-maven-plugin) in pom.xml : as you may be having multiple main classes in final jar.
<configuration><mainClass>any of the main class here</mainClass></configuration>
Invoke the Main class
java -cp final.jar -Dloader.main=<fullyqualifed_mainclass> org.springframework.boot.loader.PropertiesLauncher
By this you can invoke any of the main class from final jar.
Related
We have an zip artifact hosted on our local repository. Within my Maven project I would like to retrieve this zip dependency.
From command line I can do that via:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
-DrepoUrl=https://artifactory.company.net/artifactory/releases\
-Dartifact=com.company.api:my-swagger-doc:$VERSION:zip:resources
This will download the my-swagger-doc-2.5.3-resources.zip. I don't know how to do this in XML in my pom.xml.
My goal is to generate Spring controllers from this swagger file.
My question is:
How can I download this zip artifact and let it extract in my target directory?
You can use unpack goal of Maven Dependency Plugin.
Find below an example snippet:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<type>zip</type>
<outputDirectory>target/</outputDirectory>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
In my example, I have attached the execution to generate-resources phase. Depending on your build, you may have to attach the execution of this goal to the appropriate phase.
Could anyone provide simple configuration of Jersey and Embedded Tomcat in:
1) pom.xml
2) main method
I was trying to find it in google search, but they propose to use grizzly, glassfish and etc.
Thanks a lot.
I would go for webapp-runner to run the web app. It deploys the maven generated war from /target into a built in tomcat container. This is how java applications gets deployed on Heroku.
pom plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>7.0.34.1</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Dependency:
<dependency>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>7.0.34.1</version>
<scope>provided</scope>
</dependency>
to run the application after mvn clean package
java -jar target\dependency\webapp-runner.jar target\myapp.war
I have ProjectA which uses the Maven assembly plugin to pack some resources from a module into the repository.
I have then ProjectB which has a dependency on ProjectA. In ProjectB, I'd like to use the maven-dependency-plugin to unpack the module resources(packed by the assembly plugin) into some target folder of my choice.
I configured the dependency plugin as following, but when I run maven, it will only copy the module's resources and not also the assembly resources.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>path.to.projectA.groupId</groupId>
<artifactId>moduleA</artifactId>
<version>1.0</version>
<outputDirectory>some/path/here</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
You would need to specify the correct classifier and type to maven so that it can do this. Your assembly should available in your local repository or downloadable from repository.
For instance, assuming your assembly was named moduleA-distribution-1.0.zip, you would alter the above snippet as follows:
<artifactItem>
<groupId>path.to.projectA.groupId</groupId>
<artifactId>moduleA</artifactId>
<version>1.0</version>
<classifier>distribution</classifier>
<type>zip</type>
<outputDirectory>some/path/here</outputDirectory>
</artifactItem>
I have a jar in my maven repository that contains junit tests, which should be run in different projects, because it is able to inspect the project and test for certain features of it. Unforunately surefire doesn't pick up tests that are contained in a jar, as this Feature Request shows.
In the feature request they propose to unpack the jar to be then executed by surefire.
I successfully unpacked the jar using the maven-dependency-plugin, but the contained tests are not executed anyway. This is how I configured the maven-dependency-plugin to unpack my jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>de.mwx.test</groupId>
<artifactId>selenium-test-base</artifactId>
<version>0.1</version>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.directory}/classes
</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Any help would be appriciated.
There is a way of running a test in maven from another jar.
from maven-surefire-plugin version 2.15 you can tell maven to scan your test jars for tests and run them.
You don't need to extract the tests jar.
Just add a dependency to your test jar and:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<dependenciesToScan>
<dependency>test.jar.group:test.jar.artifact.id</dependency>
</dependenciesToScan>
</configuration>
</plugin>
Took this stuff from https://gist.github.com/aslakknutsen/4520226
And https://issues.apache.org/jira/browse/SUREFIRE-569
As expected, this works for JUnit and Testng. Will probably work for anything that surefire can run.
(This is just restating what is in a comment above from khmarbaise, but since it wasn't clarified, I think it's worth restating):
Use the test-classes directory instead of the classes folder as outputDirectory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>de.mwx.test</groupId>
<artifactId>selenium-test-base</artifactId>
<version>0.1</version>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.directory}/test-classes
</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
As described in the issue you need to have a Suite which is contains in your project which is NOT located in the test jar.
I have a project, in which I want to invoke another Jar file in M2 repo during the post execution phase of the current project.
Sample skeleton of my POM
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>exec-one</id>
<phase>verify</phase>
<configuration>
executable>java</executable>
<arguments> <argument>-jar</argument>
<argument>JarToInvoke.jar</argument>
</arguments>
<**workingDirectory**>/C:/path to repo</workingDirectory>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<dependencies> <dependency>
<groupId>GroupId of JarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
I tried with maven-exec-plugin, but having the following issues;
Where I need to specify to JarToInvoke dependency ? As a project dependency or as a exec-plugin dependency ?
With hard coding the working directory(/C:/path to repo), I am able to invoke the JarToInvoke artifact. But it is not a good solution, because finally this project should run in any m/c with different OS's. So how can I make the exec-plugin to search for the JarToInvoke artifact in the M2 repo of the project(default classpath) ?
3.While hard coding the M2 repo path in the working directory, I was able to invoke the JarToInvoke artifact. But while running the JarToInvoke artifact, it throws another dependency issue, some of the log4j dependencies to the JarToInvoke could not find. I made the JarToInvoke as a shaded jar and it work as expected. But it is not a permanent or good solution(Because the shaded jar size is of 35 MB). How can I instruct the exec-plugin to look for the dependent Jars in M2 repo.
Please share your suggestions. Thanks in Advance.
This example page from the Exec plugin's documentation describes what you want I think.
If you could use the exec:java goal instead of exec:exec, finding the JVM is taken care of for you. You can also pull in either plugin dependencies or project dependencies by changing the includeProjectDependencies and includePluginDependencies configuration options of the plugin.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>exec-one</id>
<phase>verify</phase>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>GroupId of JarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
</executableDependency>
<!-- Look up the main class from the manifest inside your dependency's JAR -->
<mainClass>com.example.Main</mainClass>
<arguments>
<!-- Add any arguments after your JAR here --->
</arguments>
</configuration>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>GroupId of JarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
The only disadvantage is that you have to explicitly specify the main class in the JAR to run. You can look this up by opening up the manifest in the dependency JAR and read the Main-Class attribute.
If you really need to use exec:exec, you could use the Maven Dependency Plugin's copy-dependencies goal to copy dependencies from your local repository to a predefined location (such as ${project.build.directory}/exec-jars) and then you can feed this directory in the exec plugin's workingDirectory configuration option.
Probably an easier way to locate the absolute path to the jar file would be to use maven-dependency-plugin with properties goal.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>exec-one</id>
<phase>verify</phase>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>${GroupIdofJarToInvoke:JarToInvoke:jar}</argument>
</arguments>
<workingDirectory>/C:/path to repo</workingDirectory>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<dependencies>
<dependency>
<groupId>GroupIdofJarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependencies>