My maven project contains two src packages and several dependencies.
I packaged it as a war and included al dependencies like libs in WEB-INF/libs.
Now I need to include inside a jar all compiled project classes from com.path.to.package.a, but I also need to exclude compiled classes contained in com.path.to.package.b.
I know I can use the <archiveClasses> option like:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archiveClasses>true</archiveClasses>
</configuration>
</plugin>
This puts correctly the jar project inside the libs, but obviously it keeps all .class files.
I tried to perform the exclusion with some options like <packagingExcludes> or <warSourceExcludes>, but no luck with those.
Is there any option I can use paired with <archiveClasses> in order to exclude form the jar what i need?
Since it seams there isn't yet a solution like the one I was looking for, I ended up with the quick and dirty following one:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>delete-unused-classes</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete includeemptydirs="true">
<fileset dir="${project.build.outputDirectory}/com/path/to/pachage/a" />
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Doing this way the unwanted classes are deleted before they are packaged from the <archiveClasses> and they do not appear inside the jar lib.
Related
I am working in Java maven environment, in my application I am generating some java classes using a SomeFileName.wsdl file. For this I have added maven plugin to pom.xml, following are the plugins,
<plugins>
<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>${project.build.directory}/generated-sources/folder-name</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<id>some-id</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>src/main/webapp/WEB-INF/wsdl</folder-name>
<wsdlFiles>
<wsdlFile>SomeFileName.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>/WEB-INF/wsdl/*</wsdlLocation>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
My question around this is, whenever this plugin generates java classes under target/generated-sources/folder-name, is there any maven plugin or maven goal or some other way available so that I can bundle this classes into a jar and can be able to add that jar to my class-path(build-path). So that, I can be able to access those generated classes from newly generated jar.
In simple words, currently using wsdl plugin classes are getting generated into target folder where I have specified my location. I just want to bundle those generated classes into a jar and add that jar to a buildpath, is there anyway to achieve this?
I have used jax-ws in some maven projects, and the class files from the generated stubs will simply be generated in the target folder, just like other class files. The generated sources config only affects the generated sources. The .class files will end up in your package structure. My suggestion is to add the packageName config, so your generated classes will be in a more convenient package. Once you build your project and the wsdl is imported successfully, you should see your .class files in the targer folder. After that, the jar packaging will go as any other project. Here is an example configuration (very similar to yours):
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<packageName>com.your.package</packageName>
<sourceDestDir>target/generated-sources/jaxws</sourceDestDir>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
I have created a application. In this application I have doubt that when getting output I need to include lib (library folder which includes all supporting jar files) but I need in a single jar files which must be include all supporting jars. Is it possible? I am using Netbeans.
You don't tell us what you are using to build your code. I'm going to guess at maven (because I know the answer is in Maven!). What you need is:
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- Add your other plug-ins here -->
</plugins>
</build>
I'm using Intellij and a coworker is using Eclipse. There is a datamodel project that most components depend on that has all the JPA code.
One of datamodel dependencies is utils. In utils there are generated sources. My coworker in Eclipse, adds the target/generated-sources of utils to the build path and everything builds and runs fine within Eclipse.
In Intellij, when I go to Project Structure, do I need to go to utils and add the target/generated-sources of utils as a Source folder to be equivalent?
Or do I need to add that module as a dependency?
Edit:
In utils pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="target/generated-sources" />
<exec executable="protoc">
<arg value="--java_out=target/generated-sources" />
<arg value="src/main/resources/utilities.proto" />
</exec>
</tasks>
<sourceRoot>target/generated-sources</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
You could use utils which were built by your coworker as a dependency.
But if you ever want to change sources of utils then you should fix its pom.xml by adding:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
IntelliJ supports the plugin and the generated-sources folder will be marked as Source folder after clicking on Reimport.
That depends if the project you build locally actually generates the sources. One instance Intellij picked up the generated sources automatically for me. If Intellij does not do it automatically then you should it as a source folder manually.
If your project doesn't generate the sources then you should add them as a dependency.
So in your case the decision is already made in the utils dependency/project. Does the utils dependency/project generate the sources or does it contain the sources?
I have a Spring Boot project and I'm building the jar file with mvn clean build.
I need to copy a folder and a file to the root of the jar, where META-INF is located. I tried maven-resources-plugin but I can't reach my goal.
For war files I used maven-war-plugin in the past but I can't find something similar for jars.
Can anybody give me an idea?
Thanks.
I could manage to add files after the repackage goal of the spring-boot-maven-plugin using the maven-antrun-plugin and the Jar tool included in the JDK.
Updating a JAR File
The Jar tool provides a u option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files.
The basic command for adding files has this format:
jar uf jar-file input-file(s)
https://docs.oracle.com/javase/tutorial/deployment/jar/update.html
Using Maven-Antrun-Plugin
This command can be executed using the maven-antrun-plugin together with the exec task, which allows you to execute commands via Runtime.exec(..).
The entry may look like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<exec executable="jar" vmlauncher="false">
<arg value="uf" />
<arg value="${project.build.directory}/myprogram.jar" />
<arg value="-C" />
<arg value="${project.build.directory}/classes" />
<arg value="org/company/app/Main.class" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Notice the -C option, which allows you to change the directory that should not be included in the jar.
https://maven.apache.org/plugins/maven-antrun-plugin/usage.html
https://ant.apache.org/manual/Tasks/exec.html
Using Exec-Maven-Plugin
Alternatively the exec-maven-plugin can be used which does not require Ant to be installed.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<configuration>
<executable>jar</executable>
<arguments>
<argument>uf</argument>
<argument>${project.build.directory}/myprogram.jar</argument>
<argument>-C</argument>
<argument>${project.build.directory}/classes</argument>
<argument>org/company/app/Main.class</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
The final structure may look like this:
myprogram.jar
|-BOOT-INF/..
|-META-INF/..
|-org/springframework/boot/loader/..
|-org/company/app/Main.class
That way you can add any additional files you want after the jar has been packaged.
could something like the following help?
See also maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>**/cdi/*</include>
<include>**/META-INF/*</include>
<include>*.properties</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
I resolved my problem by using the maven-assembly-plugin. I created an assembly zip which contains the application jar and some other resources.
This solution is specific for applications which use AWS Elastic Beanstalk and need to implement the Procfile (http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html#java-se-procfile).
i am creating a standalone java application with maven, and i am including the dependencies in the jar file with maven-dependecy-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>theMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
this includes the dependencies in the generated jar file in a lib folder, and the jar runs and works fine, but the issue is in the other generated jar file appname-1.0-jar-with-dependencies.jar.
ISSUE: i am not sure if it's an issue or not, but i noticed in target folder in the generated appname-1.0-jar-with-dependencies.jar, that it contains duplicate application files like:
all sql,property files,xml files exists twice.
all java classes .class file exists twice.
there are lots of overview.html and license.txt files related to the dependencies.
i am not sure if that's feels right or not, also i need someone to clarify for me what is the importance of this generated jar file, since i am not familiar with this plugin.
please advise, thanks.
Since you have mentioned jar-with-dependencies, I assume you are using maven assembly plugin to assemble your project artifacts along with the dependant jars into a single jar.
I suspect that your project artifacts are getting into the jar-with-dependencies twice - due to a property useProjectArtifact of dependencySet which is true, by default. You can set this property to true in your assembly descriptor and see if it addresses your issue.
In the specific case above, maven dependency plugin does not seem to be doing anything useful. maven assembly plugin automatically packages all its dependencies into a single distribution as per configuration.
But do note classpath issues if you create an executable jar-with-dependencies. You may want to create a zip or tar.gz instead.
The configuration used above is the default and does not allow for customization. You may want to use an assembly descriptor file, where you can set the property mentioned earlier or other options.