copy resources with output path containing part of input path - maven

Say two modules mod1 and mod2 have the following structure:
root
├── mod1/src/main/resources/db-migrations
| ├── v1
| | ├── a.sql
| | └── b.sql
| └── v2
| ├── c.sql
| └── d.sql
|
└── mod2/src/main/resources/db-migrations
├── v1
| ├── e.sql
| └── f.sql
└── v2
├── g.sql
└── h.sql
I want to copy all files from db-migrations into a single top-level directory, but grouped by version first, and by module second. So the output should look like this:
root/all-db-migrations
├── v1
| ├── mod1
| | ├── a.sql
| | └── b.sql
| └── mod2
| ├── e.sql
| └── f.sql
└── v2
├── mod1
| ├── c.sql
| └── d.sql
└── mod2
├── g.sql
└── h.sql
If the directory structure wasn't inversed (module name before version), this is easy with the maven resource plugin by just copying the entire db-migrations directory for each module:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-database-migrations</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>../all-db-migrations/${project.artifactId}
</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/db-migrations</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
However, I could not find a solution to do such a copy operation as described above.

Related

Maven Sonar Scanner Exclude a directory

I am trying to exclude a directory of generated source code files from being analyzed by the Maven Sonar Scanner plugin.
My folder structure looks like this:
├── processors
│   └── src
│   └── main
│   └── java
│   └── org
│   └── protobuf
└── schemas
└── src
└── main
└── java
└── org
└── generated
└── models
└── ModelA.java
└── ModelB.java
I'm trying to exclude everything under schemas.
When I run the command I do the following:
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar
-Dsonar.projectKey=myProjectKey
-Dsonar.projectName=myProjectName
-Dsonar.login=myLoginKey
-Dsonar.password=myPassword
-Dsonar.language=java
-Dsonar.sources=.
-Dsonar.exclusions=schemas/**/*
-Dsonar.junit.reportPaths=target/surefire-reports
-Dsonar.jacoco.reportPaths=target/jacoco-ut.exec
The output says:
[INFO] Excluded sources:
[INFO] schemas/**/*
...
[INFO] 2 files indexed
[INFO] 0 files ignored because of inclusion/exclusion patterns
I'd like to exclude that entire directory, and all of it's contents. How do I get sonar scanner to do this?

Configure Intellij Idea to import generated files

I'm having trouble to import a generated package with Intellij, however without any changes to the default settings it works on Eclipse.
Here is my architechture:
├── build.properties
├── pom.xml
├── README.md
├── src
│   ├── main
│   │   ├── java
│   │      └── mypackage
│   └── test
└── target
├── classes
│ └── mypackage
| └── generated
├── generated-sources
├── generated-test-sources
├── APP.jar
└── test-classes
I have most of my classes in com.mypackage however some of them are generated in
└── target
├── classes
└── mypackage
named as com.mypackage.generated and i have to use these classes in com.mypackage:
├── src
├── main
├── java
└── mypackage
However intellij cannot resolve symbol generated when I'm doing
import com.mypackage.generated
I tried to make it work by looking at the project structure/modules/dependencies menu but it seems to be for external modules. How can I do this ?
Actually it was very simple, I only was about marking classes as sources root. I don't know why marking generated as sources root did not work.

Gradle scripting: copy content of child folders and additional folder to new locations

I have the following folder structure (there is an arbitrary number of child folder and the names are not known). I only have the path to the parent folder available.
Parent
| Child_folder_0
| Child_folder_N
as well as a separate folder called contentFolder
I am trying to copy the each child folder (and it's content) into a different location as well as copy the content of contentFolder into each child folder's new location.
Thanks!
The following code:
def parentFolder = 'Parent'
def contentFolder = 'contentFolder'
def destDir = 'destDir'
task copyChildFilesFromParent(type: Copy) {
from(parentFolder) {
include '**/*'
}
into destDir
}
task copyContentFilesIntoChildren() {
(parentFolder as File).eachDir { childDir ->
copy {
from(contentFolder) {
include '**/*'
}
into "$destDir/${childDir.name}"
}
}
}
task copyFiles(dependsOn: ['copyChildFilesFromParent', 'copyContentFilesIntoChildren'])
Will transform the following directory structure:
├── build.gradle
├── contentFolder
│   ├── content.txt
│   └── data.dat
└── Parent
├── Child_folder_0
│   ├── 0.dat
│   └── data.0
├── Child_folder_1
│   ├── 1.dat
│   └── data.1
├── Child_folder_2
│   ├── 2.dat
│   └── data.2
└── Child_folder_N
├── data.N
└── N.dat
into
├── destDir
│   ├── Child_folder_0
│   │   ├── 0.dat
│   │   ├── content.txt
│   │   ├── data.0
│   │   └── data.dat
│   ├── Child_folder_1
│   │   ├── 1.dat
│   │   ├── content.txt
│   │   ├── data.1
│   │   └── data.dat
│   ├── Child_folder_2
│   │   ├── 2.dat
│   │   ├── content.txt
│   │   ├── data.2
│   │   └── data.dat
│   └── Child_folder_N
│   ├── content.txt
│   ├── data.dat
│   ├── data.N
│   └── N.dat

maven assembly plugin: control over directories

I'm trying to convert some groovy (gradle) code to maven. Where possible, we're trying to use off-the-shelf plugins rather than custom ones.
We wanted to use the maven assembly plugin to assemble a tar file that we'll use for deployment. The directory structure is important for our deployment tools, and I seem to be fighting against getting maven to get it to do what I want.
The key problem on the bottom code snippet is the fact that the jar ended up in a target directory in the tar file. My question is: can this be avoided? or should I cut my losses and write a simple custom plugin to do this?
(its possible I'm putting 2 and 2 together and getting 5, but it does seem related to this bug here)
Directory structure (After running the build)
.
└── project1
├── config
│   └── foo.config
├── pom.xml
├── src
│   └── main
│   ├── assembly
│   │   └── assembly.xml
│   └── java
│   └── com
│   └── foo
│   └── bar
│   └── App.java
└── target
├── archive-tmp
├── classes
│   └── App.class
├── maven-archiver
│   └── pom.properties
├── my-static-jar-name-bundle.tar
└── my-static-jar-name.jar
Assembly file
<assembly>
<id>bundle</id>
<formats>
<format>tar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<outputDirectory>/spooge</outputDirectory>
<includes>
<include>**/*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/config</directory>
<outputDirectory>appconfig</outputDirectory>
<includes>
<include>**/*.config</include>
</includes>
</fileSet>
</fileSets>
</assembly>
contents of the tar file when the build has finished (note the jar is in a 'target' subfolder)
tar xvf project1/target/my-static-jar-name-bundle.tar
x spooge/target/my-static-jar-name.jar
x appconfig/foo.config
Try changing the fileset as shown:
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/spooge</outputDirectory>
<includes>
<include>**/*.jar</include>
</includes>
</fileSet>
The way it's currently configured, the assembly plugin is searching project1s entire directory for any jar files. The only one it finds is in the target directory, so Maven happily includes it in the tar. The change I suggest uses project1/target as the starting point, so I think you'll get the result you want.

JBoss maven archtype: webapp-ear deployment issue

I used the maven archetype "org.jboss.spec.archetypes:jboss-javaee6-webapp-ear-archetype" to generate the code. I started JBoss 7 and then tried to deploy from project root.
mvn package
mvn jboss-as:deploy
Packaging was successful.
But, I'm getting the following error message in the deployment stage:
[ERROR] Failed to execute goal on project myProject-web: Could not
resolve dependencies for project
com.example:myProject-web:war:1.0-SNAPSHOT: Could not find artifact
com.example:myProject-ejb:jar:1.0-SNAPSHOT -> [Help 1]
I tried packaging from both project root and myProject-ear folders.
My environment: ubuntu 12.04, Oracle JDK 7, JBoss 7.1, Maven 3
Thank you very much!
This is my project structure after packaging (some parts omitted)
.
├── myProject-ear
│   ├── pom.xml
│   ├── src
│   │   └── main
│   │   └── application
│   │   └── META-INF
│   │   └── myProject-ds.xml
│   └── target
│   ├── application.xml
│   ├── maven-archiver
│   │   └── pom.properties
│   ├── myProject
│   │   ├── META-INF
│   │   │   ├── application.xml
│   │   │   └── myProject-ds.xml
│   │   ├── myProject-ejb-1.0-SNAPSHOT.jar => *** EJB JAR HAS GENERATED ***
│   │   └── myProject-web-1.0-SNAPSHOT.war => *** WAR HAS GENERATED ***
│   └── myProject.ear => *** EAR HAS GENERATED (contains other 2 archives) ***
├── myProject-ejb
│   ├── pom.xml
│   ├── src
│   └── target
│   ├── classes
│   │   ├── com
│   │   ├── import.sql
│   │   └── META-INF
│   │   ├── beans.xml
│   │   └── persistence.xml
│   ├── generated-sources
│   │   ├── annotations
│   │   └── test-annotations
│   ├── maven-archiver
│   │   └── pom.properties
│   ├── myProject-ejb-1.0-SNAPSHOT.jar => *** EJB JAR HAS GENERATED ***
│   └── test-classes
│   ├── arquillian.xml
│   ├── com
│   │   └── example
│   │   └── test
│   │   └── MemberRegistrationTest.class
│   ├── META-INF
│   │   └── test-persistence.xml
│   └── test-ds.xml
├── myProject-web
│   ├── pom.xml
│   ├── src
│   └── target
│   ├── classes
│   │   └── com
│   ├── generated-sources
│   │   └── annotations
│   ├── maven-archiver
│   │   └── pom.properties
│   ├── myProject-web-1.0-SNAPSHOT
│   │   ├── index.html
│   │   ├── META-INF
│   │   ├── resources
│   │   └── WEB-INF
│   │   ├── beans.xml
│   │   ├── classes
│   │   │   └── com
│   │   │   └── example
│   │   ├── faces-config.xml
│   │   └── templates
│   │   └── default.xhtml
│   ├── myProject-web-1.0-SNAPSHOT.war => ***WAR HAS GENERATED***
│   └── surefire
├── pom.xml
└── README.md
}
After attempting some more I figured out following works:
mvn clean package jboss-as:deploy
Add below lines in setting.xml file of .m2 directory then restart your IDE.
<profiles>
<profile>
<id>maven-profile</id>
<repositories>
<repository>
<id>MavenCentral-1</id>
<name>Maven Central Repository 1</name>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>MavenCentral-2</id>
<name>Maven Central Repository 2</name>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
<repository>
<id>jboss-public-repository-group-1</id>
<name>JBoss Public Maven Repository Group</name>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<updatePolicy>never</updatePolicy>
</snapshots>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
</profile>
</profiles>

Resources