How to add generated sources of dependent project to build path? - java

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?

Related

Bundling generated classes into a jar and adding it to the build class-path

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>

Copy file to jar root when building spring-boot project with Maven

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).

Maven war plugin archiveClasses but exclude a package

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.

Automatically add additional source directories to eclipse build path for maven project

I have a maven project conforming to the standard maven directory structure that has lots of unit and integration tests lying on top of each other in the src/test/java directory. I would like
An additional source directory (src/integration-test/java)
Eclipse to automatically realise there is this extra directory if possible
Unit tests run by surefire, integration tests run by failsafe
I have tried using the build-helper-maven-plugin every way I can think of yet eclipse still doesn't seem to see the new directory on the build path.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration-test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

Trying to integrate Launch4j in a Maven project using Alakai plugin

I am trying to integrate the generation of an installer as part of a maven compilation process.
I have found Alakai's plugin for Launch4j. I have create a simple Hello World application using Maven. I have tried to use configuration examples provided by Alakai, but when I compile my project, I get:
Failed to execute goal
org.bluestemsoftware.open.maven.plugin:launch4j-plugin:1.5.0.0:launch4j
(launch4j) on project Launch4j: Failed
to build the executable; please verify
your configuration. Application jar
doesnt exist. -> [Help 1]
Unfortunately, Alakai's documentation is limited and I could not find much with Googling.
Does anyone know where the Launch4j config.xml should be set? Is it within the project? Is it in a separate directory?
Do I need to use the assembly plugin?
I have installed Launch4j on my PC. Do I need to specify the installation directory in my pom.xml? If yes how?
Does anyone have an operational pom.xml sample/example to share?
Thanks.
There's no config.xml, you need to configure launch4j inside your pom.xml file.
You could use maven-assembly-plugin, but I recommend you to use maven-shade-plugin.
Don't need to specify launch4j installation, this plugin works 100% maven.
Sure. Follows the shade and launch4j configs I use, that generates two exes, one console and one gui, using different main classes:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached> <!-- Make the shaded artifact not the main one -->
<shadedClassifierName>shaded</shadedClassifierName> <!-- set the suffix to the shaded jar -->
</configuration>
</plugin>
<plugin>
<groupId>org.bluestemsoftware.open.maven.plugin</groupId>
<artifactId>launch4j-plugin</artifactId>
<version>1.5.0.0</version>
<executions>
<!-- GUI exe -->
<execution>
<id>l4j-gui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<outfile>target/app-gui.exe</outfile>
<jar>target/${artifactId}-${version}-shaded.jar</jar> <!-- 'shaded' is the value set on shadedClassifierName above -->
<errTitle>App Err</errTitle>
<classPath>
<mainClass>package.AppGUI</mainClass>
</classPath>
<icon>src/main/resources/icons/exeIcon.ico</icon>
<jre>
<minVersion>1.5.0</minVersion>
<maxVersion>1.6.0</maxVersion>
<initialHeapSize>128</initialHeapSize>
<maxHeapSize>1024</maxHeapSize>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>1.0.0.0</txtFileVersion>
<fileDescription>Desc</fileDescription>
<copyright>C</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>1.0.0.0</txtProductVersion>
<productName>Product</productName>
<internalName>Product</internalName>
<originalFilename>App.exe</originalFilename>
</versionInfo>
</configuration>
</execution>
<!-- Command-line exe -->
<execution>
<id>l4j-cli</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<outfile>target/app-cli.exe</outfile>
<jar>target/${artifactId}-${version}-shaded.jar</jar> <!-- 'shaded' is the value set on shadedClassifierName above -->
<errTitle>App Err</errTitle>
<classPath>
<mainClass>package.AppCLI</mainClass>
</classPath>
<icon>src/main/resources/icons/exeIcon.ico</icon>
<jre>
<minVersion>1.5.0</minVersion>
<maxVersion>1.6.0</maxVersion>
<initialHeapSize>128</initialHeapSize>
<maxHeapSize>1024</maxHeapSize>
</jre>
</configuration>
</execution>
</executions>
</plugin>
Alternatively, You can omit the 'jar' tag on launch4j-plugin and remove the extra configs of the shade-plugin, but be aware that this will replace the main jar of the flow (without embedded dependencies) by the shaded jar (with embedded dependencies), and this one will be installed on your local repo, or used in the reactor if needed.
For how to define the main class for the shade plugin, see http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html.

Categories

Resources