I am trying to download a .xlsx file into my project's resources using the following execution:
<execution>
<id>${id}</id>
<phase>generate-sources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>${url}</url>
<outputFileName>${fileOutput}</outputFileName>
<outputDirectory>${licensesDir}/em</outputDirectory>
</configuration>
</execution>
However, the file that it generates is empty and cannot be read. I know it's not a problem with the file I'm trying to download because whenever I download it directly and copy it into my project manually, I can read it it fine, and it works with my code. It also downloads other file types just fine.
Yet, whenever I try to do download it this way, in the pom.xml, the generated file is empty and my code generates a NullPointerException exception when run.
Could anyone provide some help please?
What you need is Maven Download Plugin: https://github.com/maven-download-plugin/maven-download-plugin
Add plugin to your pom and execute goal like this:
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>install-jbpm</id>
<phase>pre-integration-test</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>http://downloads.sourceforge.net/project/jbpm/jBPM%203/jbpm-3.1.4/jbpm-3.1.4.zip</url>
<unpack>true</unpack>
<outputDirectory>${project.build.directory}/jbpm-3.1.4</outputDirectory>
<md5>df65b5642f33676313ebe4d5b69a3fff</md5>
</configuration>
</execution>
</executions>
</plugin>
Related
I have a pom file that correctly generates the grpc and protobuf source files I need in target/generated-sources when run from the command line. But when I build in vscode those directories are empty and references to the protobufs are undefined. Here's the section of my pom file that builds the grpc source.
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
I added the following based on what I read in other posts to prevent vscode/eclipse from removing the generated source directories
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/protobuf/grpc-java</source>
<source>/target/generated-sources/protobuf/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
When vs-code builds, the target generated-source directories are there, but there is no source in them. We had a similar problem with intellij but were able to work around it by running the maven command line build before opening intellij but that does not seem to work for vscode.
Use protoc-jar-maven-plugin instead.
Sample usage please view protoc-jar-maven-plugin.
I'm trying to download a .xlsx file using the following plugin, in my project's pom.xml file:
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>getExcelFile</id>
<phase>generate-sources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>http://example-url.xlsx</url>
<outputFileName>file.xlsx</outputFileName>
<outputDirectory>${Dir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
However, the file becomes corrupted once downloaded and is empty. It isn't a problem with the file, as whenever I download it from the web and open it manually, it is fine.
Can someone help me out please?
For my small Java/Maven application , I am using JaCoCo in my POM.xml as under:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco.exec</destFile>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
As long as I dont use the <destFile> parameter, the default reports in target/site/jacoco folder are correctly generated in XML, CSV and HTML format. But when I use the <destFile> element in order to change the default folder where the reports are generated, only the jacoco.exec file is generated and nothing else. How can I change the report folder as well as get the reports in csv, xml and html formats?
With the destFile parameter, you changed the location where the prepare-agent goal will write the execution data file. By default, this is ${project.build.directory}/jacoco.exec, meaning (still by default) target/jacoco.exec. However, the report goal expects the path to the execution file to be passed in the dataFile parameter, which, of course, defaults to ${project.build.directory}/jacoco.exec, so that they are in sync. Therefore, if you want to change the path to this execution file, you need to match those two parameters. In order not to duplicate the path, you can use a Maven property to do that:
<properties>
<jacoco.execution.file>${project.build.directory}/coverage-reports/jacoco.exec</jacoco.execution.file>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<configuration>
<destFile>${jacoco.execution.file}</destFile>
<dataFile>${jacoco.execution.file}</dataFile>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note that this will not change the output directory of the JaCoCo reports; this just changes the path to the execute file. For that, you can use the outputDirectory parameter:
Output directory for the reports. Note that this parameter is only relevant if the goal is run from the command line or from the default build lifecycle. If the goal is run indirectly as part of a site generation, the output directory configured in the Maven Site Plugin is used instead.
and have the following configuration element added:
<configuration>
<!-- rest of your JaCoCo configuration -->
<outputDirectory>${project.build.directory}/coverage-reports/jacoco</outputDirectory>
</configuration>
This will make sure all of the HTML, XML, and CSV reports are generated under target/coverage-reports/jacoco. Take note that this configuration will not be used as part of the Maven Site generation when launching mvn site. During site generation, you'll need to configure the outputDirectory of the maven-site-plugin instead.
I am having problems building a maven project. I have a requirement to produce deterministic jar files, which must be binary-consistent across different builds and versions, in case there are no source code changes in between these builds. For the purpose, I have used this article for guidance.
I have successfully managed to build my jars and they are consistent up to my requirements. Here is my configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>step-1-remove-timestamp</id>
<phase>prepare-package</phase>
<configuration>
<target>
<touch datetime="01/01/2015 00:10:00 am">
<fileset dir="target/classes"/>
<fileset dir="src"/>
</touch>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>step-3-rename-assembly</id>
<phase>package</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}-deterministic.zip"
tofile="${project.build.directory}/${project.build.finalName}.jar"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>step-2-make-assembly</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
In the above code I build and package the jar as a zip, then copy the zip over the expected jar artifact.
The problem with the above, is that maven still executes the maven-jar-plugin, so the manually assembled jar is overwritten by the one of the maven-jar-plugin. I do not want to use this jar, since it is not consistent with my requirements.
So, I have disabled the maven-jar-plugin execution, by explicitly setting it to run for an invalid phase, like below: (saw that from other posts here)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-jar</id>
<phase>never</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
Everything seems fine, until I discovered my main jar artifact is never installed in the .m2 directory. To correct this, I also added the maven-helper-plugin so that I manually attach any artifacts I produce from the assembler plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>attach-instrumented-jar</id>
<phase>verify</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.build.finalName}.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
This leads to an error I am unable to solve:
[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact (attach-instrumented-jar) on project my-project: Execution attach-instrumented-jar of goal org.codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact failed: For artifact {full-name-of-my-project.jar}: An attached artifact must have a different ID than its corresponding main artifact. -> [Help 1]
Is there a way to overcome this issue? I've checked for solutions, and most recommend using classifiers, but I want to install the main artifact as would the maven-jar-plugin. Other software we are developing will require the standard jar dependency, and we want to avoid complicating our setup by introducing classifiers unreasonably.
After some more trial and failures I happened to come with a working solution.
I am posting it here with the hopes to either be useful, or to have any issues with pointed to me, as I am not really confident if this is a reliable approach.
So, the error I received
An attached artifact must have a different ID than its corresponding main artifact.
meant to me that I cannot manually install "again" the main artifact. Since that artifact is not produced anymore by the maven-jar-plugin, it never gets scheduled for installation even if the file is present (the antrun copy task produces a jar with the same name).
Surprisingly, it needed a few little tricks to make this work again:
Re-enabled the maven-jar-plugin as it should be:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
</execution>
</executions>
</plugin>
This will produce the standard jar on the package phase, and most importantly, makes maven aware for it to be installed during the install phase.
Tweak the maven-antrun-plugin copy tasks to overwrite the already produced jar with the deterministic zip. The setup is nearly identical as in my question, so I am only adding the differences:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>...</execution>
<execution>
<id>step-3-rename-assembly-and-sources</id>
<phase>package</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}-deterministic.zip"
tofile="${project.build.directory}/${project.build.finalName}.jar"
overwrite="true"/>
</target>
</configuration>
</execution>
. . .
</executions>
</plugin>
The copy operation now has overwrite="true" specified. Originally, the copy operation seemed to ignore files in the destination if they already exist, and what happened is that the maven-jar-plugin had already produced the default jar artifact when the copying occured. With this option set, the maven-antrun-plugin now overrides the former jar with the deterministic one, and the latter becomes a subject of the maven install phase.
Removed the setup from the build-helper-maven-plugin, so that the main jar artifact is not copied a second time:
<artifact>
<file>${project.build.directory}/${project.build.finalName}.jar</file>
<type>jar</type>
</artifact>
That's it, the correct jar is installed in the .m2 dir.
In case you don't have a plugin to create and attach main artifact for you, there is a more generic solution with Groovy Maven plugin:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>set-main-artifact</id>
<phase>package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.artifact.setFile(new File(project.build.directory, project.build.finalName + ".zip"))
</source>
</configuration>
</execution>
</executions>
</plugin>
Inspired by post https://stackoverflow.com/a/31513690/2053580 many thanks to https://stackoverflow.com/users/1314907/lukasz-guminski
I have a project named HelloWorld. It is a maven java project.
I have got a repository like https://my.repo.com/svn/filesToCopy
What i want to do is:
Automaticly coping 3 files (a.xsd, b.xsd, c.xsd) from SVN repository into my src/main/resource directory.
I was trying to add smth like that in my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun-plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Coping one file</echo>
<copy file="https://my.repo.com/svn/filesToCopy/a.xsd" todir="${basedir}/src/main/resource" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
And then just building a project but it isnt working at all.
What is more im wondering if passing a https link need a username and password somewhere.
Any ideas?
Have you tried this approach? It's from Maven SCM plugin
<execution>
<id>perform-checkout</id>
<configuration>
<connectionUrl>myUrl</connectionUrl>
<checkoutDirectory>myDirectory</checkoutDirectory>
<excludes>folder1</excludes>
</configuration>
<phase><!-- some phase --></phase>
<goals>
<goal>checkout</goal>
</goals>
</execution>