maven download plugin to add downloaded resource to jar - java

I am using download-maven-plugin to download some of resources to be used in my project. The download is successful and I can use the downloaded file.
However, what I want is to include the downloaded file in the JAR.
Note : The resource file will be included in the JAR when it was pre-downloaded before running the build, but if it is not present (e.g. deleted or I want to update it perhaps) the resource file is not included in the JAR.
Basically what I want is to include the recently downloaded file (from mvn clean install) in the output JAR.
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://downloadurl</url>
<outputFileName>error.json</outputFileName>
<outputDirectory>${project.resources.dir}/commons</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Same thing happens when I use maven-ant-run plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>download-files</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<get src="https://downloadurl"
dest="${project.resources.dir}/commons/data.json"
verbose="true"
usetimestamp="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

Just download it to ${basedir}/target/classes before packaging phase. You don't need it to be in sources in order to include it to JAR.

Solved!
I changed the phase to process-resources..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>download-files</id>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<get src="https://downloadUrl"
dest="${project.resources.dir}/commons/error.json"
verbose="true"
usetimestamp="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

Related

org.apache.axis2 maven plugin, not picking up the proper wsdlFile

Using org.apache.axis2 maven plugin, I am trying to generate stubs out of two WSDL files. I have mentioned the wsdl file names in to <wsdlFile>My/WSDL/File</wsdlFile>. The plugin is still picking up the default WSDL file name /src/main/resources/service.wsdl. Since my wsdlFile names are not service.wsdl, I am getting java.io.FileNotFoundException. Please help me to change the wsdlFile name to my required wsdlFile name.
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.7.9</version>
<executions>
<execution>
<id>ws1</id>
<goals>
<goal>wsdl2code</goal>
</goals>
<configuration>
<databindingName>xmlbeans</databindingName>
<wsdlFile>src/main/resources/service1.wsdl</wsdlFile>
<outputDirectory>target/generated-sources</outputDirectory>
</configuration>
</execution>
<execution>
<id>ws2</id>
<goals>
<goal>wsdl2code</goal>
</goals>
<configuration>
<databindingName>xmlbeans</databindingName>
<wsdlFile>src/main/resources/service2.wsdl</wsdlFile>
<outputDirectory>target/generated-sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

How to run embedded TomEE for integration tests with test resources

I have J2EE project based on maven. This project contains connection to database, which is set up via resources.xml and persistence.xml. Connection works fine for normal deployment.
My problem is, that i would like to run embedded TomEE server for integration tests. For these tests i need use inmemory database.
To start TomEE i use combination of maven plugins showed below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>7.0.4</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<checkStarted>true</checkStarted>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<simpleLog>true</simpleLog>
</configuration>
</plugin>
When i start maven goal mvn install, server runs as expected, but with wrong DB connection. I did not find way, how to set up, that i need use src/test/resources instead of src/main/resources.
Do you know how to set up this plugin in way? I'am open to suggestions of similar solutions, which is simple and do not contains 'lot of' frameworks.
Thank you in advance.
After some investigation i found solution described below.
Three plugins were added into pom.
First plugin will start TomEE server for us.
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>7.0.4</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<checkStarted>true</checkStarted>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<simpleLog>true</simpleLog>
</configuration>
Second one will replace proper resources and persistence files.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>copy-test-persistence</id>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<!--backup the "proper" files-->
<copy file="${project.build.outputDirectory}/META-INF/resources.xml" tofile="${project.build.outputDirectory}/META-INF/resources.xml.proper"/>
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml.proper"/>
<!--replace the "proper" files with the "test" version-->
<copy file="${project.build.testOutputDirectory}/META-INF/resources.xml" tofile="${project.build.outputDirectory}/META-INF/resources.xml"/>
<copy file="${project.build.testOutputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>restore-persistence</id>
<phase>prepare-package</phase>
<configuration>
<tasks>
<!--restore the "proper" files -->
<copy file="${project.build.outputDirectory}/META-INF/resources.xml.proper" tofile="${project.build.outputDirectory}/META-INF/resources.xml"/>
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml.proper" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
Last plugin force 'clean' before install. Without this i had problem in case, when i forget clean project before install.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>

Maven zip artifact copy, unzip and rename

I have a maven module which is packaged as a zip with naming my-artifact-1.0-SNAPSHOT.zip using maven-shade-plugin.
Once the artifact has been installed into local maven repo, I need:
Copy zip to configured local folder.
Unzip archive.
Rename unzipped folder from my-artifact-1.0-SNAPSHOT to my-artifact.
This process should be cross-platform working on Windows, Linux, MacOS machines.
I read that it could be accomplished by using:
maven-dependency-plugin plus another plugin
maven-groovy-plugin
What would the best way to implement such a flow? Any examples are very appreciated.
No need for antrun in my opinion, the maven-dependency-plugin should do the trick:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>validate</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[your.group.id]</groupId>
<artifactId>my-artifact-1.0-SNAPSHOT</artifactId>
<version>[your.version]</version>
<type>zip</type>
<outputDirectory>${project.basedir}/my-artifact/</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Regards,
Tom
You have a maven plugin that can execute Ant-Tasks. This tasks let you execute a lot of works. For example, to unzip a file:
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<tasks>
<unzip src="path/to/zip/file.zip" dest="path/to/unzip" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
You can check all availables task in the apache ant page (see references).
Reference:
https://ant.apache.org/manual/Tasks

Create symlink to the current jar package after mvn package

My jar file name is configured in pom.xml like this:
<finalName>MyApp-${project.version}</finalName>
I would like Maven ('mvn package') to create a symlink to the current jar file every time I run it, so that I have two files:
MyApp-1.0.3.jar
MyApp.jar (which is "ln -s MyApp-1.0.3.jar MyApp.jar")
You can use Exec Maven Plugin as below to achieve this goal:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.6.0</version>
<executions>
<execution>
<id>Version Calculation</id>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>ln</executable>
<arguments>
<argument>-fnsv</argument>
<argument>target/MyApp-${project.version}.jar</argument>
<argument>MyApp.jar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Use below goals to execute it:
mvn clean verify
You can use symlink task/goal of ant-run plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<symlink link="${project.build.directory}/${project.artifactId}.jar"
resource="${project.build.directory}/${project.artifactId}-${project.version}.jar"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
If you want the symlinks to have relative path, you can give relative path in resource like below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<symlink link="${project.build.directory}/${project.artifactId}.jar"
resource="./${project.artifactId}-${project.version}.jar"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

How do I prevent a ZIP file created by maven from being deployed?

I'm using the maven-dependency-plugin to gather all of the dependencies for my application and then package them up in a ZIP file with the maven-assembly-plugin. That part is all working great.
However, when I do mvn deploy, it deploys the ZIP file to my Archiva server, which I don't want. I'd just like to have the JAR's deployed there individually.
Is there any way to prevent the upload of the ZIP file?
Here are the relevant sections of my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>myProject-${project.version}</finalName>
<outputDirectory>../</outputDirectory>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Try adding
<attach>false</attach>
to maven-assembly-plugin's configuration section. This should prevent the generated ZIP from being deployed. See http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html

Categories

Resources