How to start java app from Maven in non-base project folder? - java

I'm using Intellij Idea 14 and start learning embedded Maven installation.
At now Maven compile sources, generate JAR with MANIFEST.MF inside (with 'lib/' prefixes in 'Class-Path') to 'target/', copy dependencies at 'target/lib/' folder, copy folder 'src/main/resources/cfg' to 'target/cfg'.
If i manually go to folder 'target' and start app with 'java -jar app.jar' then it works fine and successfully find folder 'cfg', but if i start app from Idea then app triyng to search folder 'cfg' in base project folder.
There is some POM piece:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<workingDirectory>${project.build.directory}\</workingDirectory>
<mainClass>app.Main</mainClass>
</configuration>
</plugin>
I looked at workingDirectory parameter [ http://www.mojohaus.org/exec-maven-plugin/exec-mojo.html ]. This won't work.
How can i change working folder to 'target'? This is for comfort app develop and run/debug right from Idea.
I need to get that result application structure:
/app.jar
/cfg
/cfg/application.cfg

Related

How to find resource path in maven multimodule application

I have a multimodule maven application that has the following structure:
main-project
->submodule1
->src->main
->java
->MainClass.java
->resource
->php
index11.php
file12.php
file13.php
->submodule2
->src->main
->java
MainClass.java
->resource
->php
index21.php
file22.php
file23.php
->submodule3
->src->main
->java
MainClass.java
->resource
->php
index31.php
file32.php
file33.php
->web-app
->src->main
->webapp
Java classes from submodules should access the php files in their resource directories and execute it using Quercus Resin. However, when the project is packed in war, submodules are packed into jar file that are stored in web-app/WEB-INF/lib, which makes it impossible to execute php files. As workaround for this problem, I found solution to copy all php files from the submodules into the web-app, so when it's extracted in Tomcat, it's not inside jar file and could be executed. For that purpose, I'm using maven-remote-resources-plugin, and all php files are stored to web-app/src/main/webapp/php.
The problem I have now is how to properly provide path to these php files from java classes inside submodules.These java classes are inside jar files when application is deployed to Tomcat, but during development I'm using embedded Jetty server, so I need solution that would work in both cases.
If I use class loader to get resource,e.g. getClass().getClassLoader().getResource("/php/index11.php").getPath() it returns absolute path to the submodule1.jar file.
Any idea how to solve this issue?
I managed to solve this problem, so I will post solution here if it might help someone else.
In each submodule I have a maven-remote-resources-plugin bundle to collect all resources I need
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*.php</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Then, in web-app submodule I'm using maven-remote-resources-plugin process to copy these php files to resource directories called WEB-INF/php/submodule-name
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>org.au.morph.offline:morph-sample:${project.version}</resourceBundle>
<resourceBundle>org.au.morph.offline:morph-project:${project.version}</resourceBundle>
</resourceBundles>
<outputDirectory>src/main/webapp/WEB-INF</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
Finally, I created a utility method that resolves the correct path to this directory both when I run application from IDE or in the Tomcat:
public static String getWebContentPath(String contextPath) throws UnsupportedEncodingException {
String path = PathUtils.class.getClassLoader().getResource("").getPath();
String fullPath = URLDecoder.decode(path, "UTF-8");
if(fullPath.contains("/WEB-INF/classes")){
String pathArr[] = fullPath.split("/classes/");
fullPath=pathArr[0];
}
String reponsePath = "";
reponsePath = new File(fullPath).getPath() + File.separatorChar + "php"+File.separatorChar+contextPath;
return reponsePath;
}

Adding source Folders to my Eclipse Project at generate-sources

Before redirecting me to one of the numerous other posts concerning this (like this or this), know that I have already tried that.
What I want to do is simple: I have a folder called generated-sources on the same level as src and I want maven to automatically add it to the source directories.
Here is the snippet as it can also be seen in the questions I linked above:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>generated-sources/enums/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
When I run it, it actually displays the lines
[INFO] --- build-helper-maven-plugin:1.8:add-source (add-source) # gui ---
[INFO] Source directory: C:\development\gdg\gui\generated-sources\enums added.
Thing is, even if I do this, the source directories don't appear in the Project Explorer and I can't use them as dependencies either.
Note: I am currently using a m2e Connector Plugin and already tried using the <pluginManagement> tag. I also already reloaded the Project, yet to no avail.
I am probably just being incredibly stupid again, so thanks for any help.
~Crowley
Try a Maven > Update Project ..., besides reloading the project.
If that doesn't help, you can always manually add sources to a project: under a project's Properties, in Java Build Path, Sources tab, use the Add Folder button to select your folder.

appassembler-maven-plugin add custom entry to classpath

I'm writing a Java-Application which is using some libaries from anonther thirdparty-Application which is running on my server. At the moment im building my App with the appassembler-maven-plugin. This plugin copy my jars (app and dependencies) into the lib folder an generates a shellscript in the bin dir.
The classpath is generated in this shellscirpt. This solution works but i dublicate the dependency-jars (on time in my app and in the thirdparty-Application write the app for). The classpath of my thirdparty-application is set in a systemvariable like $THIRDPARTYAPP_CLASSPATH.
I want to set the dependencies in my pom.xml to provided, so that the appassembler don't add them to lib and classpath and want to add the systemvar $THIRDPARTYAPP_CLASSPATH in my shellscript, so that my app uses the jars from the installed thirdparty-application.
At the moment i'm doing this manually (editing the shellscript after the build) and it works. Is there any method in the appassembler-maven-plugin to add thid systemvar to the classpath automatically?
I couldn't find anything in the documentation and other questions here regarding a similar problem are not well answerd.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.8.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<assembleDirectory>${project.build.directory}/appassembler</assembleDirectory>
<extraJvmArguments>-Xms512m -Xmx1024m</extraJvmArguments>
<generateRepository>true</generateRepository>
<repositoryName>lib</repositoryName>
<repositoryLayout>flat</repositoryLayout>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<platforms>
<platform>unix</platform>
</platforms>
<programs>
<program>
<mainClass>${mainClass}</mainClass>
<id>app</id>
</program>
</programs>
</configuration>
</plugin>
You can configure to create an <environmentSetupFileName>setup-env</environmentSetupFileName> which can define a new classpath part via CLASSPATH_PREFIX which should solve your problem.

Include the external jar files in my project

I'm developing a Java SE application with Netbeans IDE.
I added external jar files to the project.
After my project is compiled, these external jar files will be in the lib folder, next to the jar file.
I want to use jar in jar solution. So the external jar files are included in my project's runnable jar file.
Is any standard method to solve this problem? Or this is not a good solution?
Thank you!
I'm pretty sure that Java doesn't support embedded Jars. I'd recommend moving the lib out to the file system (so that it creates a sub folder ./lib within the same drctory as your executable Jar & see if that makes a difference.
If you really want to use embedded Jars, you could take a read of One-Jar
You should have a directory structure of
Example below
.\JarTest2.jar
.\lib\log4j-1.2.17.jar
You might wanna try One-JARâ„¢; and if your project is Maven-based:
<build>
<plugins>
<!-- other plugins omitted for brevity -->
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<attachToBuild>true</attachToBuild>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

creating jar file of maven projects with netbeans

I am using netbeans to work on a project. I am using svn, so that I can commit to newer versions and revert to an older version in case I mess up. Now I want to create a jar file of this project but the build tab in the project properties does not display the packaging option, it only displays one option and that is of 'compile'.
If I create a new project (Java Application) it does show the packaging option and then I can easily create a jar file, but this maven project that I am working on does not work like this.
Please suggest me a way to create jar file out of my maven project.
Thanks,
Based on #yatskevich answer, you could go to your NetBeans Project Properties > Actions and add package to the Execute Goals of the following Actions:
Build project
Clean and Build project
Build with dependencies
Plus any other you feel should also package
I did this on NetBeans 7.2.1
PS: this will create the JAR on every change you make, so choose wisely where to add package. I don't mind it building a JAR for small projects on every build.
Open cmd (if you are on Windows) or any shell (if you are on Linux)
Navigate to your project directory (use cd command)
Run mvn clean package there.
Your jar will be in <project dir>/target.
Include maven-assembly-plugin plugin in your .pom file. It will instruct Maven to assemble your application with all it's dependencies.
Later when you will build your Netbeans project you will see your newly builded jar with all it's dependencies.
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.my.class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Check out to learn more about creating Jar file with Maven: https://javatutorial.net/create-java-jar-file-with-maven

Categories