├── .gitignore ├── README.md └── settings.zip /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Intellij IDEA LiveTemplates 2 | 3 | These are my custom [IntelliJ IDEA Live Templates](https://www.jetbrains.com/help/idea/using-live-templates.html) for Java that I am currently using. 4 | 5 | ## Export and Import 6 | 7 | You can import LiveTemplates by File -> Manage IDE Settings -> Import Settings -> Select settings.zip 8 | 9 | You can export LiveTemplates by File -> Manage IDE Settings -> Export Settings -> Select Live Templates -> Ok 10 | 11 | ## Java 12 | 13 | 1. `log` - Adds SLF4J Logger 14 | 15 | ```java 16 | private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS$.class); 17 | ``` 18 | 19 | 20 | 2. `at` - AssertJ assertThat() 21 | 22 | ```java 23 | org.assertj.core.api.Assertions.assertThat($END$) 24 | ``` 25 | 26 | ## JPA 27 | 28 | 1. `jpa-entity` - JPA Entity 29 | 30 | ```java 31 | @jakarta.persistence.Entity 32 | class $entity$ { 33 | 34 | @jakarta.persistence.Id 35 | @jakarta.persistence.GeneratedValue 36 | private java.lang.Long id; 37 | private java.lang.String name; 38 | 39 | public $entity$() {} 40 | 41 | public $entity$(String name) { 42 | this.name = name; 43 | } 44 | 45 | public java.lang.Long getId() { 46 | return id; 47 | } 48 | 49 | public void setId(java.lang.Long id) { 50 | this.id = id; 51 | } 52 | 53 | public java.lang.String getName() { 54 | return name; 55 | } 56 | 57 | public void setName(java.lang.String name) { 58 | this.name = name; 59 | } 60 | 61 | @java.lang.Override 62 | public java.lang.String toString() { 63 | return "$entity${" + 64 | "id=" + id + 65 | ", name='" + name + '\'' + 66 | '}'; 67 | } 68 | } 69 | ``` 70 | 71 | 2. `jpa-entity-lombok` - JPA Entity with Lombok 72 | 73 | ```java 74 | @lombok.Setter 75 | @lombok.Getter 76 | @lombok.AllArgsConstructor 77 | @lombok.NoArgsConstructor 78 | @jakarta.persistence.Entity 79 | class $name$ { 80 | @jakarta.persistence.Id 81 | @jakarta.persistence.GeneratedValue 82 | private Long id; 83 | private String name; 84 | } 85 | ``` 86 | 87 | 3. `jpa-entity-id-seq` - JPA Entity Id of Sequence Generator Type 88 | 89 | ```java 90 | @jakarta.persistence.Id 91 | @jakarta.persistence.GeneratedValue(strategy = jakarta.persistence.GenerationType.SEQUENCE, generator = "$table$_id_generator") 92 | @jakarta.persistence.SequenceGenerator(name = "$table$_id_generator", sequenceName = "$table$_id_seq", allocationSize = 50) 93 | private Long id; 94 | ``` 95 | 96 | 4. `jpa-many-to-many` - JPA Entity property of ManyToMany Type 97 | 98 | ```java 99 | @jakarta.persistence.ManyToMany(cascade = {jakarta.persistence.CascadeType.PERSIST, jakarta.persistence.CascadeType.MERGE}) 100 | @jakarta.persistence.JoinTable( 101 | name = "$join_table_name$", 102 | joinColumns = {@jakarta.persistence.JoinColumn(name = "$id_column$", referencedColumnName = "$id_ref_column$")}, 103 | inverseJoinColumns = {@jakarta.persistence.JoinColumn(name = "$id_join_column$", referencedColumnName = "$id_ref_join_column$")}) 104 | private Set<$CLASS$> $name$; 105 | 106 | ``` 107 | 108 | ## SpringBoot 109 | 110 | 1. `boot-controller` - SpringMVC REST Controller 111 | 112 | ```java 113 | @org.springframework.web.bind.annotation.RestController 114 | @org.springframework.web.bind.annotation.RequestMapping("/api") 115 | class $CLASS$ { 116 | 117 | } 118 | ``` 119 | 120 | 2. `boot-service` - SpringBoot Service 121 | 122 | ```java 123 | @org.springframework.stereotype.Service 124 | @org.springframework.transaction.annotation.Transactional(readOnly=true) 125 | public class $CLASS$ { 126 | 127 | } 128 | ``` 129 | 130 | 3. `boot-app-properties` - SpringBoot ApplicationProperties binding class 131 | 132 | ```java 133 | @org.springframework.boot.context.properties.ConfigurationProperties(prefix = "$prefix$") 134 | public class $CLASS$ { 135 | 136 | } 137 | ``` 138 | 139 | 4. `boot-clr` - SpringBoot CommandLineRunner 140 | 141 | ```java 142 | @org.springframework.stereotype.Component 143 | public class $CLASS$ implements org.springframework.boot.CommandLineRunner { 144 | 145 | @Override 146 | public void run(String... args) throws Exception { 147 | 148 | } 149 | } 150 | ``` 151 | 152 | 5. `boot-add-cors-mappings` - SpringBoot WebMvcConfig addCorsMappings 153 | 154 | ```java 155 | @Override 156 | public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) { 157 | registry.addMapping("/api/**") 158 | .allowedMethods("*") 159 | .allowedHeaders("*") 160 | .allowedOriginPatterns("*") 161 | .allowCredentials(true); 162 | } 163 | ``` 164 | 165 | 6. `boot-cors-filter` - SpringBoot CORS Filter 166 | 167 | ```java 168 | @org.springframework.context.annotation.Bean 169 | public org.springframework.boot.web.servlet.FilterRegistrationBean simpleCorsFilter() { 170 | org.springframework.web.cors.UrlBasedCorsConfigurationSource source = new org.springframework.web.cors.UrlBasedCorsConfigurationSource(); 171 | org.springframework.web.cors.CorsConfiguration config = new org.springframework.web.cors.CorsConfiguration(); 172 | config.setAllowCredentials(true); 173 | config.setAllowedOrigins(java.util.Collections.singletonList("http://localhost:4200")); 174 | config.setAllowedMethods(java.util.Collections.singletonList("*")); 175 | config.setAllowedHeaders(java.util.Collections.singletonList("*")); 176 | source.registerCorsConfiguration("/**", config); 177 | org.springframework.boot.web.servlet.FilterRegistrationBean bean = new org.springframework.boot.web.servlet.FilterRegistrationBean(new org.springframework.web.filter.CorsFilter(source)); 178 | bean.setOrder(org.springframework.core.Ordered.HIGHEST_PRECEDENCE); 179 | return bean; 180 | } 181 | ``` 182 | 183 | 7. `boot-open-api-config` - SpringBoot OpenAPI Configuration 184 | 185 | ```java 186 | import io.swagger.v3.oas.models.ExternalDocumentation; 187 | import io.swagger.v3.oas.models.OpenAPI; 188 | import io.swagger.v3.oas.models.info.Info; 189 | import io.swagger.v3.oas.models.info.License; 190 | import io.swagger.v3.oas.models.security.SecurityScheme; 191 | import org.springframework.context.annotation.Bean; 192 | import org.springframework.context.annotation.Configuration; 193 | 194 | @Configuration 195 | public class $CLASS$ { 196 | @Bean 197 | public io.swagger.v3.oas.models.OpenAPI springShopOpenAPI() { 198 | return new io.swagger.v3.oas.models.OpenAPI() 199 | .info(info()) 200 | .externalDocs(externalDocumentation()) 201 | .schemaRequirement("bearerAuth", jwtSecurityScheme()) 202 | ; 203 | } 204 | 205 | private io.swagger.v3.oas.models.info.Info info() { 206 | return new io.swagger.v3.oas.models.info.Info() 207 | .title("API Title") 208 | .description("API description") 209 | .version("v0.0.1") 210 | .license(new io.swagger.v3.oas.models.info.License().name("MIT")); 211 | } 212 | 213 | private io.swagger.v3.oas.models.ExternalDocumentation externalDocumentation() { 214 | return new io.swagger.v3.oas.models.ExternalDocumentation() 215 | .description("API Documentation") 216 | .url("https://github.com/user/repo/README.md"); 217 | } 218 | 219 | private io.swagger.v3.oas.models.security.SecurityScheme jwtSecurityScheme() { 220 | io.swagger.v3.oas.models.security.SecurityScheme scheme = new io.swagger.v3.oas.models.security.SecurityScheme(); 221 | scheme.setName("bearerAuth"); 222 | scheme.setType(io.swagger.v3.oas.models.security.SecurityScheme.Type.HTTP); 223 | scheme.setScheme("bearer"); 224 | scheme.setBearerFormat("JWT"); 225 | return scheme; 226 | } 227 | } 228 | ``` 229 | 230 | 8. `boot-mvc-test` - SpringMVC Test method 231 | 232 | ```java 233 | @org.springframework.beans.factory.annotation.Autowired 234 | private org.springframework.test.web.servlet.MockMvc mockMvc; 235 | 236 | @org.junit.jupiter.api.Test 237 | void test$Function$() throws java.lang.Exception { 238 | var request = org.springframework.test.web.servlet.request.MockMvcRequestBuilders 239 | .post("") 240 | .contentType(org.springframework.http.MediaType.APPLICATION_JSON); 241 | 242 | mockMvc.perform(request) 243 | .andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status().isCreated()) 244 | .andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content().contentType(org.springframework.http.MediaType.APPLICATION_JSON)) 245 | .andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("", org.hamcrest.Matchers.is(""))); 246 | } 247 | ``` 248 | 249 | 9. `boot-dynamic-property-registrar` - Spring Boot DynamicPropertyRegistrar 250 | 251 | ```java 252 | @org.springframework.context.annotation.Bean 253 | org.springframework.test.context.DynamicPropertyRegistrar dynamicPropertyRegistrar() { 254 | return (registry) -> { 255 | registry.add("", ""); 256 | }; 257 | } 258 | ``` 259 | 260 | 10. `boot-datasource-jpa-properties` - Spring Boot DataSource and JPA Properties 261 | 262 | ```properties 263 | spring.datasource.url=jdbc:postgresql://localhost:5432/postgres 264 | spring.datasource.username=postgres 265 | spring.datasource.password=postgres 266 | spring.jpa.open-in-view=false 267 | spring.jpa.hibernate.ddl-auto=validate 268 | spring.jpa.show-sql=true 269 | #spring.jpa.properties.hibernate.format_sql=true 270 | ``` 271 | 272 | ## Testcontainers 273 | 274 | 1. `tc-magic-jdbc-url` - Spring Boot TestPropertySource with Testcontainers magic JDBC URL 275 | 276 | ```java 277 | @org.springframework.test.context.TestPropertySource(properties = { 278 | "spring.test.database.replace=none", 279 | "spring.datasource.url=jdbc:tc:postgresql:17-alpine:///db" 280 | }) 281 | ``` 282 | 283 | 2. `tc-postgres` - PostgreSQLContainer 284 | 285 | ```java 286 | @org.testcontainers.junit.jupiter.Container 287 | static org.testcontainers.containers.PostgreSQLContainer postgres = 288 | new org.testcontainers.containers.PostgreSQLContainer<>("postgres:17-alpine"); 289 | ``` 290 | 291 | ## SQL 292 | 293 | 1. `flyway-table` - Flyway table creation script 294 | 295 | ```sql 296 | create sequence $table$_id_seq start with 1 increment by 50; 297 | 298 | create table $table$ ( 299 | id bigint not null default nextval('$table$_id_seq'), 300 | created_at timestamp not null default now(), 301 | updated_at timestamp, 302 | primary key (id) 303 | ); 304 | ``` 305 | 306 | ## Maven 307 | 308 | 1. `jib-maven-plugin` - Add Jib Maven Plugin 309 | 310 | ```xml 311 | 312 | com.google.cloud.tools 313 | jib-maven-plugin 314 | 3.3.4 315 | 316 | 317 | eclipse-temurin:21-jre 318 | 319 | 320 | sivaprasadreddy/${project.artifactId} 321 | 322 | latest 323 | ${project.version} 324 | 325 | 326 | 327 | 328 | 8080 329 | 330 | 331 | 332 | 333 | ``` 334 | 335 | 2. `git-commit-id-maven-plugin` - Adds git-commit-id-maven-plugin 336 | 337 | ```xml 338 | 339 | io.github.git-commit-id 340 | git-commit-id-maven-plugin 341 | 342 | 343 | 344 | revision 345 | 346 | 347 | 348 | 349 | false 350 | false 351 | true 352 | 353 | ^git.branch$ 354 | ^git.commit.id.abbrev$ 355 | ^git.commit.user.name$ 356 | ^git.commit.message.full$ 357 | 358 | 359 | 360 | ``` 361 | 362 | 3. `spotless-maven-plugin` - Adds spotless-maven-plugin 363 | 364 | ```xml 365 | 366 | com.diffplug.spotless 367 | spotless-maven-plugin 368 | 2.44.2 369 | 370 | 371 | 372 | 373 | 374 | 2.50.0 375 | 376 | 377 | 378 | 379 | 380 | 381 | compile 382 | 383 | check 384 | 385 | 386 | 387 | 388 | ``` 389 | 390 | 4. `frontend-maven-plugin-npm` - Adds frontend-maven-plugin with NPM 391 | 392 | ```xml 393 | 394 | com.github.eirslett 395 | frontend-maven-plugin 396 | 1.15.1 397 | 398 | ${project.basedir}/../frontend 399 | v20.14.0 400 | 401 | 402 | 403 | install node and npm 404 | 405 | install-node-and-npm 406 | 407 | generate-resources 408 | 409 | 410 | npm install 411 | 412 | npm 413 | 414 | generate-resources 415 | 416 | install 417 | 418 | 419 | 420 | npm build 421 | 422 | npm 423 | 424 | generate-resources 425 | 426 | 427 | true 428 | 429 | run build 430 | 431 | 432 | 433 | 434 | ``` 435 | 436 | 5. `frontend-maven-plugin-yarn` - Adds frontend-maven-plugin with YARN 437 | 438 | ```xml 439 | 440 | com.github.eirslett 441 | frontend-maven-plugin 442 | 1.15.1 443 | 444 | 445 | install node and yarn 446 | 447 | install-node-and-yarn 448 | 449 | 450 | ${project.basedir}/../frontend 451 | v20.14.0 452 | v1.22.22 453 | 454 | 455 | 456 | yarn install 457 | 458 | yarn 459 | 460 | generate-resources 461 | 462 | install 463 | 464 | 465 | 466 | yarn run build 467 | 468 | yarn 469 | 470 | 471 | run build 472 | 473 | 474 | 475 | 476 | ``` 477 | 478 | 6. `maven-resources-plugin` - Adds maven-resources-plugin 479 | 480 | ```xml 481 | 482 | org.apache.maven.plugins 483 | maven-resources-plugin 484 | 3.3.1 485 | 486 | 487 | Copy UI build content to public 488 | generate-resources 489 | 490 | copy-resources 491 | 492 | 493 | ${project.build.directory}/classes/public 494 | true 495 | 496 | 497 | ${project.basedir}/../frontend/build 498 | 499 | 500 | 501 | 502 | 503 | 504 | ``` 505 | 506 | ## Docker 507 | 508 | 1. `boot-Dockerfile` - Creates multistage Dockerfile for Spring Boot application 509 | 510 | ``` 511 | # the first stage of our build will extract the layers 512 | FROM eclipse-temurin:21-jre as builder 513 | WORKDIR application 514 | ARG JAR_FILE=target/*.jar 515 | #ARG JAR_FILE=build/libs/*.jar 516 | COPY ${JAR_FILE} application.jar 517 | RUN java -Djarmode=layertools -jar application.jar extract 518 | 519 | # the second stage of our build will copy the extracted layers 520 | FROM eclipse-temurin:21-jre 521 | WORKDIR application 522 | COPY --from=builder application/dependencies/ ./ 523 | COPY --from=builder application/spring-boot-loader/ ./ 524 | COPY --from=builder application/snapshot-dependencies/ ./ 525 | COPY --from=builder application/application/ ./ 526 | ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"] 527 | ``` -------------------------------------------------------------------------------- /settings.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sivaprasadreddy/intellij-live-templates/6e211765f13a8b20577d431a40178d41a2d3be68/settings.zip --------------------------------------------------------------------------------